[php-src] Issue #7740: Access behavior of SimpleXMLElement changes when element has sibling
| From: | noreply@php.net | Date: | Wed, 08 Dec 2021 15:04:33 +0000 |
| Subject: | [php-src] Issue #7740: Access behavior of SimpleXMLElement changes when element has sibling | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-238285@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7740
Author: LeonMelis
### 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
<?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