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

Your name:

Reply:


Posted by Schraalhans Keukenmeester on 06/11/07 18:58

At Mon, 11 Jun 2007 14:46:45 +0200, gosha bine let h(is|er) monkeys type:

> On 11.06.2007 06:46 phpCodeHead wrote:
>> 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
>> LAMP Web Developer
>> bizFlowDesigns.com
>>
>
> This is a new "feature" of 5.2 - for some mystical reason they've
> removed implicit object-to-string conversion. You must provide explicit
> __toString if you're printing or concatenating your objects.

What should the contents of $judy be after assigning

$judy = new Person('Judy').'\n';

be in your opinion?
'Judy\n' ?
'Object\n' ?
'sprinkled icecream\n' ?
'3.14159265\n' ?

Imho implicit obj2str conversion is meaningless, and though I have abused
the construct myself a few times I am glad they corrected this behaviour.
Having to define your own stringifier forces you to think about and
implement what is logically the proper meaning of a conversion to an
otherwise incompatible type.

I am not sure how many scripts out in the field rely on such implicit
conversion but I do sympathize with you; it's frustrating at times to find
yet another changed property between point releases. But don't you agree
it's still better to have to change some code to something more correct
than to have to deal with all kinds of odd behaviour?

Sh.



--
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

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