IMSoP / simplexml_debug

Tools for debugging your code when working with PHP's SimpleXML functions.
MIT License
52 stars 16 forks source link

Can't distinguish single nodes from lists with one item #2

Open IMSoP opened 12 years ago

IMSoP commented 12 years ago

There is currently no way of distinguishing between a single element (e.g. $sxml->Result[0]) and a list of elements (e.g. $sxml->Result or $sxml->children()) which happens to include only one item. They can however behave differently - foreach ( $sxml->Result[0] as $result ) is equivalent to foreach ( $sxml->Result[0]->children() as $result ).

As far as I can see, there is no way of determining this with the current API. However, since the behaviour is different, the extension itself clearly tracks this distinction. It should probably be raised with the maintainers to add PHP access to this property for a future release.

IMSoP commented 11 years ago

hakre has some interesting tricks for spotting different object types here: http://hakre.wordpress.com/2013/02/12/simplexml-type-cheatsheet/

hakre commented 9 years ago

Haha, just was stumbling over your README because I linked to the project in a recent SO answer and then stumbled over that and thought: mhm, that should be answered. And yes it is, I've also created an example here as a gist.

Execute it on 3v4l.org, results were:

Output for 5.2.2 - 7.0.0beta3, hhvm-3.3.1 - 3.8.1

For the single element:
  Is single-element?   - Yes
  Is list-of-elements? - No

For the list of elements with a single item:
  Is single-element?   - No
  Is list-of-elements? - Yes

Do not use it with a PHP version before 5.2.2.