|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26962 __toString is not called during (string) cast and inside an expression
Submitted: 2004-01-19 05:01 UTC Modified: 2004-07-28 23:24 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: alex_mailbox53 at yahoo dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.0 OS: *
Private report: No CVE-ID: None
 [2004-01-19 05:01 UTC] alex_mailbox53 at yahoo dot com
The ZEND_CHANGES defines that __toString method whould 
be called whenever object is casted to sting. But actually it is 
casted to string only during print (echo) operator. 
Even the example in ZEND_CHANGES does not work: 
            class Foo { 
                function __toString() { 
                    return "What ever"; 
            $obj = Foo; 
            $str = (string) $obj; // call __toString() 
            echo $obj; // call __toString() 
print $str will print the "Object" sring, not the "What ever". 
One more code example: 
class Integer { 
    private $value; 
    function __construct($val) { 
        $this->value = $val; 
    function __toString() { 
        return (string)($this->value); 
$i = new Integer(10); 
if (10 == $i) 
    echo '10!!!! :-)'; 
"10!!! :-)" is not printed. 

Expected result:
When object is accessed in expressions it should be explicitly 
casted to sting nor vartype. 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-19 13:07 UTC]
Please try using this CVS snapshot:
For Windows:
 [2004-01-24 23:59 UTC]
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.

 [2004-07-28 22:40 UTC] Maik_Heller at hotmail dot com
problem still exists even with lates build (for win)
 [2004-07-28 23:01 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

For 5.0.x this is expected behavior. __toString() will only called automatically for echo and print constructs. 

Maybe we\'ll change that for 5.1.
 [2004-07-28 23:24 UTC]
Actually this is a PEAR problem. PEAR is not compatible with PHP 5. It is designed for PHP 4 only.
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC