php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25640 simplexml element object properties return empty objects
Submitted: 2003-09-23 21:25 UTC Modified: 2004-01-08 15:45 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: tater at potatoe dot com Assigned: sterling (profile)
Status: Not a bug Package: XML related
PHP Version: 5* OS: *
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:
34 - 14 = ?
Subscribe to this entry?

 
 [2003-09-23 21:25 UTC] tater at potatoe dot com
Description:
------------
I guess this is related to the recent toString() stuff -
simplexml_element object properties can't be printed or
assigned, they all come out as 'Object #x'. the string
values turn into empty simplexml_element objects.

Reproduce code:
---------------
<?php
$xml = '<wrapper><foo>s1</foo><bar>s2</bar><bar>s3</bar></wrapper>';
$t = simplexml_load_string($xml);
var_dump($t,$t->foo,$t->bar);
?>

Expected result:
----------------
object(simplexml_element)#1 (2) {
  ["foo"]=>
  string(2) "s1"
  ["bar"]=>
  array(2) {
    [0]=>
    string(2) "s2"
    [1]=>
    string(2) "s3"
  }
}
string(2) "s1"
array(2) {
  [0]=>
  string(2) "s2"
  [1]=>
  string(2) "s3"
}

Actual result:
--------------
object(simplexml_element)#1 (2) {
  ["foo"]=>
  string(2) "s1"
  ["bar"]=>
  array(2) {
    [0]=>
    string(2) "s2"
    [1]=>
    string(2) "s3"
  }
}
object(simplexml_element)#2 (0) {
}
array(2) {
  [0]=>
  object(simplexml_element)#3 (0) {
  }
  [1]=>
  object(simplexml_element)#4 (0) {
  }
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-16 08:52 UTC] tim at timcrider dot com
I have been able to produce a similar problem, but I'm not sure if it's exactly the same bug or a variant of this one.

-----[ XML Field ]-----
<?xml version="1.0" encoding="UTF-8"?>
<test>
 <fields>
  <myField>a</myField>
  <myField>b</myField>
  <myField>c</myField>
 </fields>
</test>

-----[ Script ]-----
#!/usr/local/bin/php -q
<?php

 $sx = simplexml_load_file("_example.xml");

 print_r($sx);

 for ($i = 0; $i < count($sx->fields->myField); $i++)
 {
    $v = $sx->fields->myField[$i];

    print "True Value: {$v}\n";
    $tmp[] = $v;
    $tmp2[] = "$v";

 }

 print "Trying to view 'tmp' array:\n\n";
 print_r($tmp);

 print "Trying to view 'tmp2' array:\n\n";
 print_r($tmp2);

?>

-----[ Output ]-----
simplexml_element Object
(
    [fields] => simplexml_element Object
        (
            [myField] => Array
                (
                    [0] => a
                    [1] => b
                    [2] => c
                )

        )

)
True Value: a
True Value: b
True Value: c
Trying to view 'tmp' array:

Array
(
    [0] => simplexml_element Object
        (
        )

    [1] => simplexml_element Object
        (
        )

    [2] => simplexml_element Object
        (
        )

)
Trying to view 'tmp2' array:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

-----[ PHP Version Info ]-----
PHP 5.0.0b2 (cli) (built: Dec  7 2003 18:04:51)
Copyright (c) 1997-2003 The PHP Group
Zend Engine v2.0.0-dev, Copyright (c) 1998-2003 Zend Technologies
    with Turck MMCache v2.4.6, Copyright (c) 2002-2003 TurckSoft, St. Petersburg, by Dmitry Stogov


I tried the latest CVS, at the time of this posting, and it would not build because of a mysqli compile problem.
 [2004-01-08 14:55 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

A property access returns the element (as an object).
Using __tostring() or conversion by (string) returns the text content. If an element contains multiple elements of the same name then accessing those by proeprty returns an array of eleemnt objects. So all is fine.
 [2004-01-08 15:33 UTC] tater at potatoe dot com
This bug was not "bogus" - assignments did not work, nor did printing. There now seems to be some implicit string conversions that fix that. Which is fine, I guess.

It's still very odd, at the least, that a property access returns an empty object. Nothing else works like this. If it's intentional, it's a very peculiar and questionable intention.

Run this code:
<?php
$xml = '<wrapper><foo>s1</foo><bar>s2</bar><bar>s3</bar></wrapper>';
$t = simplexml_load_string($xml);
$x = new stdClass;
$x->foo = 's1';
$x->bar = array('s2','s3');
var_dump($t,$t->foo,$t->bar);
var_dump($x,$x->foo,$x->bar);
?>

and you really think the results are "fine"?? Whatever.

That word, "bogus", is like pissing on the people entering bugs. (Ask a native English speaker what they would think you meant if you called something they did "bogus".) If you'd rather just have a broken product, then why allow bug entries at all?
 [2004-01-08 15:41 UTC] helly@php.net
The word bogus may be bogus but hey that's an automatic reply - sorry if you get pissed of by an autoreply.
 [2004-01-08 15:45 UTC] helly@php.net
The objects are empty because they only use dynamic properties and do not declare any objects. Those properties can be seen as if they were handled by __get() and __set() in user defined classes.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 05:01:29 2024 UTC