php.net |  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
Votes:1
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:
 [2004-01-19 05:01 UTC] alex_mailbox53 at yahoo dot com
Description:
------------
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. 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-19 13:07 UTC] helly@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip
 [2004-01-24 23:59 UTC] sniper@php.net
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] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

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] helly@php.net
Actually this is a PEAR problem. PEAR is not compatible with PHP 5. It is designed for PHP 4 only.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 18:01:55 2014 UTC