Giter Site home page Giter Site logo

Comments (4)

damianwadley avatar damianwadley commented on May 14, 2024

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.

LeonMelis avatar LeonMelis commented on May 14, 2024

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.

nikic avatar nikic commented on May 14, 2024

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.

iluuu1994 avatar iluuu1994 commented on May 14, 2024

Given the feedback let's close this.

from php-src.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.