Access behavior of SimpleXMLElement changes when element has sibling
Description
This was a hard bug to name, and hence it was hard to search for. I couldn't find a similar report, but if this was already reported, my apologies.
The behavior when accessing a property in a SimpleXMLElement object seems to be influenced by the element having 'siblings' or not. This behavior can be observed with xdebug, as well as using print_r or var_dump.
In the example, when accessing $xml_correct->bar the result is as expected.
However, adding a sibling 'fiz' to element 'bar' in $xml_bug will change the result when accessing $xml_bug->bar. Suddenly, $xml_bug->bar becomes an array. It only does this when $xml_bug->bar is accessed directly. When accessing $xml_bug (or viewing it in the debugger), the 'bar' property appears as expected.
I have tested this in php 7.2, 7.3, 7.4 and 8.0, all show the same result. 3v4l.org shows the same output: https://3v4l.org/0EGhe
This might be a bug in libxml, or php, I'm not sure.
<?php
/**
* This demos a (possible) bug in PHP, which is observed in PHP 7.2, 7.3, 7.4 and 8.0 (not yet tested on 8.1).
*
* Access behaviour of a SimpleXMLElement changes if that node has a sibling.
*
* In this example, adding 'fiz' to 'foo' (which is the outer node) will change behaviour when accessing 'bar' (an inner node).
*/
$xml_correct = new SimpleXMLElement('<foo><bar><baz/></bar></foo>');
print_r($xml_correct); // As expected
print_r($xml_correct->bar); // As expected, $bar contains property $baz, which is an empty SimpleXMLElement
$xml_bug = new SimpleXMLElement('<foo><bar><baz/></bar><fiz/></foo>');
print_r($xml_bug); // As expected, property $bar is equal to that observed in print_r($xml_correct)
print_r($xml_bug->bar); // UNEXPECTED: property $bar is an array, containing one SimpleXMLElement with property $baz which is an empty SimpleXMLElement
Resulted in this output:
// output of print_r($xml_bug->bar):
SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
[baz] => SimpleXMLElement Object
(
)
)
)
But I expected this output instead:
// output of print_r($xml_correct->bar):
SimpleXMLElement Object
(
[baz] => SimpleXMLElement Object
(
)
)
PHP Version
7.4 8.0
Operating System
Linux
Note that $bar is not actually an array but a SimpleXMLElement object that, when dumped, makes it look like it could contain multiple pieces of data. ->bar will always behave as a single instance (except in dumps) and ->bar[0] will always access the first "bar" element, and both are regardless of whether there is only one value or there are multiple for that name.
https://3v4l.org/n8sJH
Is there a particular issue in code you're encountering because of this, or is it just that the output is surprising (and arguably inaccurate)?
This behavior is not only observed when being dumped or inspected via xdebug. It is actually exposed via get_object_vars, and probably some other methods as well.
Is there a particular issue in code you're encountering because of this, or is it just that the output is surprising (and arguably inaccurate)?
Yes, we discovered this behavior due to unexpected result in production code. We have code that verifies the structure of an XML file like this:
foreach (get_object_vars($simpleXMLElement) as $key => $val) {
// do validation of $key and $value
}
Here, $key could suddenly become '0' instead of 'baz', depending on whether the parent XMLElement contains more than 1 item (a sibling, so to say).
Reading your comment, it looks like we can do a workaround by always accessing an SimpleXMLElement instance as an array with one item, so with $simpleXMLElement[0], like this:
// Note: we must always treat SimpleXMLElement as an array, because
// sometimes PHP treats the object as an array internally. By accessing
// it as an array we *should* get consistent behavior.
// See: https://github.com/php/php-src/issues/7740
foreach (get_object_vars($simpleXMLElement[0]) as $key => $val) {
// do validation of $key and $value
}
But that feels like a hack. And as you can see, this would require adding comment lines to explain to anyone trying to maintain this code why this construct is needed. I cannot blame someone from being confused by this, as it is unexpected and (as far as I can tell) undocumented behavior.
I understand that this is an edge-case, and changing this behavior might actually break existing implementations. Maybe the solution should be to clarify this somewhere in the documentation, and just live with it.
I'm inclined to close this as won't fix based on previous experience trying to fix issues in the SimpleXML debug dump (which is also used by get_object_vars / array casts). The last time we tried to address an inconsistency in the SimpleXML debug output there was a big outcry about it breaking code. See https://bugs.php.net/bug.php?id=61597 and https://bugs.php.net/bug.php?id=79528.
Given the feedback let's close this.