Reply to Re: Object of class Person could not be converted to string

Your name:

Reply:


Posted by Schraalhans Keukenmeester on 06/11/07 07:19

At Mon, 11 Jun 2007 04:46:57 +0000, phpCodeHead let h(is|er) monkeys type:

> Code which should allow my constructor to accept arguments:
>
> <?php
> class Person {
> function __construct($name)
> {
> $this->name = $name;
> }
>
> function getName()
> {
> return $this->name;
> }
>
> function printName()
> {
> print $this->name;
> }
>
> private $name;
> }
>
> $judy = new Person("Judy") . "\n"; // <- this is line parser don't
> like
> $joe = new Person("Joe") . "\n";
>
> $judy->printName() . '<br />';
> $joe->printName() . '<br />';
> ?>
>
> Outputs:
>
> Catchable fatal error: Object of class Person could not be converted
> to string
>
> Anyone seen this before? Know why? Am I missing something here?
>
> Thanks all...
>
> Gene Kelley

What are you trying to achieve by concatenating "\n" to an object?
Your constructor creates an instance of your class, not a string.
Since you then concatenate it with ."\n" PHP attempts to convert the
object to a string, and fails. If you want that, you have to create your
own stringifier inside the class definition.

If you want to have the string "Judy\n" in your object, pass it as a
single string : $judy = new Person ("Judy\n");



--
Schraalhans Keukenmeester - schraalhans@the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация