|  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
Avg. Score:3.8 ± 1.3
Reproduced:36 of 41 (87.8%)
Same Version:11 (30.6%)
Same OS:8 (22.2%)
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
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'?>
$xml = simplexml_load_string($string);
$login = $xml->login;
$login = (string) $xml->login;

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

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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-03 05:23 UTC] lists at cyberlot dot net

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]
That's by design and was discussed on
 [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.
 [2015-03-22 02:07 UTC] perske at uni-muenster dot de
This behaviour *is* documented, but at a very hidden place:
On the lengthy page
in the introduction to example #6.

The bug is not the need for a cast; this need is technically well-founded, see the internal discussions. The bug is using the word "simple" in the name of a class with an interface that looks so absurd to the eyes of the usual programmer. But it is to late to fix this bug.

If you dislike casts, you can access elements and attributes without using them:
$login = $xml->login->__toString();
But (string)$xml->login and "{$xml->login}" are shorter.
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 16:01:42 2015 UTC