__toString() with cast is broken in php5 RC1

php.internals

David Giffin

22 years ago
Hi There, I just got a chance tp update to PHP5 RC1 and noticed that some things have changed for the __toString method. It seems that it it only getting called when the object is printed or echo. Casting the object to a string returns the Object #. Here is an example: ---- The Script: <?php class obj { function __toString() { return "yo!!\n"; } } $obj = new obj(); print_r($obj); $test = (string) $obj; print $test; print $obj; ?> ----- Produces: obj Object ( ) Object id #1yo!! Thanks, David

Marcus Börger

22 years ago
Hello David, that's correct. See the NEWS file, 3rd entry: 18 March 2004, PHP 5 Release Candidate 1 - Fixed numerous bugs with the just-in-time auto-global initialization, that could cause $_SERVER, $argv/$argc and other variables not to work properly. (Zeev) - Fixed data corruption with constant assignments to object properties. (Zeev) - Changed __toString() to be called automatically only with print and echo statements. (Andi) Friday, April 2, 2004, 1:13:28 AM, you wrote:
> Hi There,
> I just got a chance tp update to PHP5 RC1 and noticed that some things > have changed for the __toString method. It seems that it it only getting > called when the object is printed or echo. Casting the object to a string > returns the Object #. Here is an example:
> ---- The Script:
> <?php
> class obj > { > function __toString() > { > return "yo!!\n"; > } > }
> $obj = new obj(); > print_r($obj);
> $test = (string) $obj;
> print $test; > print $obj;
?>>
> ----- Produces:
> obj Object > ( > ) > Object id #1yo!!
> Thanks, > David
-- Best regards, Marcus mailto:helly@php.net

David Giffin

22 years ago
Hi Marcus, Is this going to change back? The php5 documentation on the zend site shows that both casting and print/echo should work. I guess people could use the output buffering methods to get the print data for the time being. A not so happy work around :( Thanks again, David On Fri, 2 Apr 2004, Marcus Boerger wrote:

Marcus Börger

22 years ago
Hello David, generally the idea is to change this back. But i don't think we will be able to solve the related problems before PHP 5.1. marcus Friday, April 2, 2004, 2:03:38 AM, you wrote:

Jevon Wright

22 years ago
Can you add a default __toString() for all objects (which don't extend or implement a __toString()) then? Because at least then we could use $str = $anyobj->__toString(). Otherwise I don't think __toString() has any real functionality, except for print and echo... Jevon ----- Original Message ----- From: "Marcus Boerger" <helly@php.net> To: "David Giffin" <david@agent911.com> Cc: "Marcus Boerger" <helly@php.net>; <internals@lists.php.net> Sent: Friday, April 02, 2004 12:27 PM Subject: Re: [PHP-DEV] __toString() with cast is broken in php5 RC1
> Hello David, > > generally the idea is to change this back. But i don't think we will > be able to solve the related problems before PHP 5.1. > > marcus > > Friday, April 2, 2004, 2:03:38 AM, you wrote: > > > > Hi Marcus, > > > Is this going to change back? The php5 documentation on the zend site > > shows that both casting and print/echo should work. > > > I guess people could use the output buffering methods to get the print > > data for the time being. A not so happy work around :( > > > Thanks again, > > David > > > > On Fri, 2 Apr 2004, Marcus Boerger wrote: > > >> Hello David, > >> > >> that's correct. See the NEWS file, 3rd entry: > >> > >> 18 March 2004, PHP 5 Release Candidate 1 > >> - Fixed numerous bugs with the just-in-time auto-global initialization,
that
> >> could cause $_SERVER, $argv/$argc and other variables not to work
properly.
> >> (Zeev) > >> - Fixed data corruption with constant assignments to object properties.
(Zeev)
> >> - Changed __toString() to be called automatically only with print and
echo
> >> statements. (Andi) > >> > >> Friday, April 2, 2004, 1:13:28 AM, you wrote: > >> > >> > >> > Hi There, > >> > >> > I just got a chance tp update to PHP5 RC1 and noticed that some
things
> >> > have changed for the __toString method. It seems that it it only
getting
> >> > called when the object is printed or echo. Casting the object to a
string

Andi Gutmans

22 years ago
I think this is an interesting idea. The downside is that __toString() would need special treatment in the engine (possibly even an extra opcode). The upside is that people could always use __toString(). Andi At 03:55 PM 4/2/2004 +1200, Jevon Wright wrote:

Derick Rethans

22 years ago
On Thu, 1 Apr 2004, David Giffin wrote:
> Is this going to change back? The php5 documentation on the zend site > shows that both casting and print/echo should work.
*casting* works fine, we just don't do it automagically: print (string) $obj; regards, Derick

Adam Maccabee Trachtenberg

22 years ago
On Fri, 2 Apr 2004, Derick Rethans wrote:
> *casting* works fine, we just don't do it automagically: > > print (string) $obj;
Not anymore: class foo { function __toString() { return "I am a string."; } } $foo = new foo; print (string) $foo; Object id #1 -adam
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!

Derick Rethans

22 years ago
On Fri, 2 Apr 2004, Adam Maccabee Trachtenberg wrote:
> On Fri, 2 Apr 2004, Derick Rethans wrote: > > > *casting* works fine, we just don't do it automagically: > > > > print (string) $obj; > > Not anymore:
Yup, I know :) Derick