php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62328 implementing __toString and a cast to string fails
Submitted: 2012-06-14 19:11 UTC Modified: 2012-08-12 04:06 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: jordan dot raub at dataxltd dot com Assigned: laruence (profile)
Status: Closed Package: SimpleXML related
PHP Version: 5.4.4 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
10 + 11 = ?
Subscribe to this entry?

 
 [2012-06-14 19:11 UTC] jordan dot raub at dataxltd dot com
Description:
------------
I would expect casting to a string to have the same result as calling the 
__toString() function on extending a SimpleXMLElement object. 
I've tested this against

PHP 5.3.3-7+squeeze13 with Suhosin-Patch (cli) (built: Jun 10 2012 07:31:32) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with XCache v1.3.0, Copyright (c) 2005-2009, by mOo
    with Xdebug v2.1.2, Copyright (c) 2002-2011, by Derick Rethans
    with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH

and 

PHP 5.4.4 (cli) (built: Jun 14 2012 11:50:02) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

and 


PHP 5.3.14 (cli) (built: Jun 14 2012 11:49:39) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies


Test script:
---------------
<?php
class UberSimpleXML extends SimpleXMLElement {
	public function __toString() {
		return 'stringification';
	}
}

$xml = new UberSimpleXML('<xml/>');
$xml->tag = "you're it!";

echo "\nno cast\n";
echo $xml;
echo "\ncast\n";
echo (string) $xml;
echo "\nexplicit call\n";
echo $xml->__toString();
echo "\n";
var_dump((string) $xml);
var_dump($xml->__toString());


Expected result:
----------------
no cast
stringification
cast

explicit call
stringification
string(0) ""
string(15) "stringification"


Actual result:
--------------
no cast
stringification
cast
stringification
explicit call
stringification
string(15) "stringification"
string(15) "stringification"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-14 19:13 UTC] jordan dot raub at dataxltd dot com
Oops! Accidentally switched the expected and actual results...
 [2012-08-10 10:17 UTC] leight at gmail dot com
This is caused by a built-in classes cast_object function being called when it 
exists, before falling back to alternative methods of converting to a string.

I have added a pull request that fixes the issue.

https://github.com/php/php-src/pull/157
 [2012-08-12 03:49 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7b307fb930e6cf328993dee4b060f6f823c39d24
Log: Fixed bug #62328 (implementing __toString and a cast to string fails)
 [2012-08-12 04:00 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7b307fb930e6cf328993dee4b060f6f823c39d24
Log: Fixed bug #62328 (implementing __toString and a cast to string fails)
 [2012-08-12 04:06 UTC] laruence@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2012-08-12 04:06 UTC] laruence@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: laruence
 [2014-10-07 23:23 UTC] stas@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=7b307fb930e6cf328993dee4b060f6f823c39d24
Log: Fixed bug #62328 (implementing __toString and a cast to string fails)
 [2014-10-07 23:34 UTC] stas@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=7b307fb930e6cf328993dee4b060f6f823c39d24
Log: Fixed bug #62328 (implementing __toString and a cast to string fails)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 22:01:28 2024 UTC