Guest Posted April 6, 2007 Share Posted April 6, 2007 Hi folks, I'm fairly certain this fits here, so here goes: I have an object oriented template system (a class named Template), and a Form object that extends Template. Both objects use the __toString() magic function, and at first, this worked. At the end of the page I'd have: echo $template; Then, sometime down the road, the script suddenly started ignoring __toString() and started putting out "Object Id #" -- the odd thing is that this change did not occur while I was editing the classes themselves. And what changes I did make, I undid (even though they shouldn't be the cause), and yet the problem remains. I can (and have) simply changed all my calls to the object to an appropriate display() function, but I was wondering if anyone may know just what may cause PHP to suddenly overlook __toString()? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/45923-script-ignoring-__tostring/ Share on other sites More sharing options...
utexas_pjm Posted April 6, 2007 Share Posted April 6, 2007 May seem obvious, but as a sanity check I would check to see that you did not inadvertentlyswitch from PHP5 to PHP4. Link to comment https://forums.phpfreaks.com/topic/45923-script-ignoring-__tostring/#findComment-223081 Share on other sites More sharing options...
Guest Posted April 6, 2007 Share Posted April 6, 2007 May seem obvious, but as a sanity check I would check to see that you did not inadvertentlyswitch from PHP5 to PHP4. That would've been quite a punch in the face if it were the case, heh! But, yep, the server's running PHP 5.1.6. I suspected the same when it first happened. Any other ideas? Link to comment https://forums.phpfreaks.com/topic/45923-script-ignoring-__tostring/#findComment-223095 Share on other sites More sharing options...
Guest Posted April 6, 2007 Share Posted April 6, 2007 Did some research and there are people who state the __toString() function isn't invoked unless used directly with print or echo. Which seems a bit odd, there are times where I didn't use print/echo and the value returned was still a string. Perhaps, in php 5.1.6, __toString()'s "priority" is lower than in 5.2? Just a guess, otherwise, does anyone really know how the PHP engine decides whether to return the object or the string value? Might be a clue. Link to comment https://forums.phpfreaks.com/topic/45923-script-ignoring-__tostring/#findComment-223101 Share on other sites More sharing options...
448191 Posted April 9, 2007 Share Posted April 9, 2007 Please check the following: 1. Do you have error_reporting set on E_ALL 2. Check your PHP ver, reinstall 5.2 if required 3. Are you trying to echo out a freshly contructed object? I.e.: echo new Class; will produce the following error: Fatal error: Method Class::__toString() must return a string value 4. Ensure __toString() is actually returning a string, use typecasting if required 5. Ensure __toString() is declared public But 3 to 5 should produce errors/notices. Only other thing I can think of: You have misspelled __toString or have declared it private, AND you are forwarding the request to to a different class using __call or because of your inheritance tree, which returns an object (bad sentence, but you get the idea). In any case, might be a good idea to share your code. Link to comment https://forums.phpfreaks.com/topic/45923-script-ignoring-__tostring/#findComment-224792 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.