php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29500 Accessing object elements returns object
Submitted: 2004-08-03 05:15 UTC Modified: 2004-08-03 15:39 UTC
Votes:49
Avg. Score:3.8 ± 1.3
Reproduced:32 of 37 (86.5%)
Same Version:10 (31.2%)
Same OS:6 (18.8%)
From: lists at cyberlot dot net Assigned:
Status: Wont fix Package: SimpleXML related
PHP Version: 5.0.0 OS: Fedora Core 2
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2004-08-03 05:15 UTC] lists at cyberlot dot net
Description:
------------
When using simplexml to access a element the returned object it returns a object instead of a string

Have to type cast it as a (string) to get the information back, Should not have to typecast.

If a typecast is required it should be specificed in the docs.

Reproduce code:
---------------
$string = <<<XML
<?xml version='1.0'?>
<document>
    <cmd>login</cmd>
    <login>Richard</login>
</document>
XML;
                                                                                                                    
$xml = simplexml_load_string($string);
print_r($xml);
$login = $xml->login;
print_r($login);
$login = (string) $xml->login;
print_r($login);


Expected result:
----------------
SimpleXMLElement Object
(
    [cmd] => login
    [login] => Richard
)
Richard
Richard

Actual result:
--------------
SimpleXMLElement Object
(
    [cmd] => login
    [login] => Richard
)
SimpleXMLElement Object
(
    [0] => Richard
)
Richard

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-03 05:23 UTC] lists at cyberlot dot net
http://us3.php.net/manual/en/ref.simplexml.php

In trying to get this working I played with the examples from this page.

Example 7 returns the following error
Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference in /root/test.php on line 29
 [2004-08-03 12:01 UTC] chregu@php.net
That's by design and was discussed on 
internals@lists.php.net
 [2004-08-03 15:29 UTC] lists at cyberlot dot net
Im not signed up for that list...
The online docs badly need updating then.

1. To express the need to use typecasting depending on how you access the variable.

2. That you can no longer "change" the simplexml object like explained in example 7
 [2004-08-03 15:39 UTC] lists at cyberlot dot net
Ok it is impossible to modify the simpleobject once creating which means you can't add anything to the xml.

Is this a desired affect as well?
 [2011-05-31 11:22 UTC] stu at dyndev dot co dot nz
This intentional bug is still there, I cannot for the life of me see why it would be that way or why it's still not documented all these years later.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 25 07:02:14 2014 UTC