php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28292 __toString() does not work in echo "x=$x"
Submitted: 2004-05-06 02:00 UTC Modified: 2004-05-07 17:08 UTC
From: php at koteroff dot ru Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.0RC2 OS: Windows
Private report: No CVE-ID: None
 [2004-05-06 02:00 UTC] php at koteroff dot ru
Description:
------------
Method __toString() work in PHP5 RC1 correctly, but in PHP4 RC2 - wrong (in today CVS - too). Bewitched method? :-) There are so many bugs and changes around them in all PHP versions... I'm finishing the book about PHP5, please say if __toString() behavour will be changed in release.

P.S.
I cannot understand too why this method is called only in print and echo functions - thanks to it I cannot write

function myecho($s) { echo $s; }
myecho("x=$x");

But, I think, it is another question.

Reproduce code:
---------------
<?
class C {
  function __toString() { return "s"; }
}
$x = new C;
echo "x=$x";
?>

Expected result:
----------------
x=s

Actual result:
--------------
x=Object id #1
(and NO warning or notice)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-06 02:33 UTC] php at koteroff dot ru
Appositely, 

echo "x=".((string)$x);

does not work too in PHP5 RC2!
 [2004-05-06 06:20 UTC] amt@php.net
This was changed due to potential bugginess in certain 
cases. It will be readdressed in PHP 5.1.
 [2004-05-07 03:36 UTC] php at koteroff dot ru
So, nowadays __toString() does not work at all and NOT planned to be fixed in near half-year?
 [2004-05-07 17:08 UTC] amt@php.net
It works when you echo or print the object. It will work 
in other cases in PHP 5.1, or sooner if you can provide 
a patch that handles all the potential bugs.

Nobody is happy about removing functionality, but it was 
decided it was better to reduce the scope of 
__toString() than to introduce bugs in PHP 5.0. The fix 
turned out to be non-trivial and not something that 
could be done at this late date in the release progress.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 18:01:35 2024 UTC