Comments (4)
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)?
from php-src.
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.
from php-src.
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.
from php-src.
Given the feedback let's close this.
from php-src.
Related Issues (20)
- password_hash does not allow \0 in the $password HOT 3
- Trait method has limited visibility to parent class method HOT 1
- AVIF support on imagecreatefromstring HOT 4
- session mode mm HOT 3
- Proposal for Standardization of PHP Library Directory Structure HOT 6
- [ HELP REQUEST ] Continue already running generator to implement middleware flow HOT 1
- Test failures on Alpinelinux using OpenSSL 3.2+ HOT 3
- PHP-FPM `ping.path` and `ping.response` config vars are ignored. HOT 2
- php_curl does not work with IIS extended protection HOT 1
- is_float(integer) == true HOT 3
- Memory leak in xml extension HOT 3
- Opcache JIT segmentation fault HOT 3
- Update compilation parameters for the intl plugin HOT 6
- Segmentation fault libpq.so - freePGconn HOT 2
- Memory leaks when opcache preloading non-exist file HOT 1
- [Chore] resolve "perpetually open" CVEs for PHP and FPM HOT 7
- Unable to load dynamic library 'mysqli' HOT 7
- `make test` Privacy Issues HOT 3
- Reflection: promoted constructor parameter has no default value HOT 2
- ZEND_MOD_CONFLICTS doesn't work
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-src.