php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48059 SimpleXmlElement __toString() not called on (string) cast
Submitted: 2009-04-23 09:27 UTC Modified: 2009-04-24 17:44 UTC
From: doctorrock83 at gmail dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.9 OS: Win32
Private report: No CVE-ID: None
 [2009-04-23 09:27 UTC] doctorrock83 at gmail dot com
Description:
------------
If I extend SimpleXmlElement and add a __toString() method to it, that method is not called when I cast my object using (string)$obj.
It is used however on implicit casts (e.g echo $sxmlObj)

Reproduce code:
---------------
class MyXml extends SimpleXMLElement 
{
    public function __toString()
    {
        return $this->asXML();
    }

    public function export()
    {
        return (string)$this;
    }
}
$xml = new MyXml("<xml><some>xml</some></xml>");
echo $xml; // 1
$a = (string)$xml;
echo $a; // 2
echo $xml->export(); // 3

Expected result:
----------------
1 :
xml returned

2 :
xml returned

3:
xml returned

Actual result:
--------------
1 :
OK

2:
nothing is returned

3:
nothing is returned

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-24 17:37 UTC] jani@php.net
See bug #46968
 [2009-04-24 17:37 UTC] jani@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Jan 28 17:03:58 2023 UTC