note 54097 added to language.oop5.magic
| From: | DomenicDenicola at osu1 dot php dot net | Date: | Thu, 23 Jun 2005 15:52:19 +0000 |
| Subject: | note 54097 added to language.oop5.magic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90847@lists.php.net to get a copy of this message | ||
This small sentence tripped me up for a half an hour:
"It is worth noting that the __toString method will only be called when it is directly combined
with echo() or print()."
So code like this will _not_ work, even though you might think it would:
<?
//$x is some variable with a __toString method defined.
$y = "x's value is: " . $x;
$y = "x's value is: " . (string)$x;
?>
In _em_ both situations, $y will contain "x's value is: Object id #42" (or whatever
object ID). So, the only recourse I guess is this:
<?
$y = "x's value is: " . $x->__toString();
?>
----
Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54097
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54097&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54097&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54097&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54097&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54097&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54097&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54097&report=yes
Search -- http://master.php.net/manage/user-notes.php