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:65
Avg. Score:3.7 ± 1.4
Reproduced:41 of 46 (89.1%)
Same Version:13 (31.7%)
Same OS:10 (24.4%)
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: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lists at cyberlot dot net
New email:
PHP Version: OS:

 

 [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

Pull Requests

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.
 [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 http://php.net/manual/en/simplexml.examples-basic.php
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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC