|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75168 Unexpected PHP Warning Node no longer exists
Submitted: 2017-09-07 12:33 UTC Modified: 2018-08-06 22:57 UTC
Avg. Score:4.4 ± 0.7
Reproduced:9 of 9 (100.0%)
Same Version:4 (44.4%)
Same OS:8 (88.9%)
From: goetas at gmail dot com Assigned:
Status: Open Package: SimpleXML related
PHP Version: 7.0.23 OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
30 - 18 = ?
Subscribe to this entry?

 [2017-09-07 12:33 UTC] goetas at gmail dot com
currently the simplexml api exposes the SimpleXMLElement class when asking elements that does not exists in the XML dom, but some operations are inconsistent.

$xml // instanceof \SimpleXMLElement

$node = $xml->NOT_EXISTING_NODE_NAME; // instanceof \SimpleXMLElement

$a = $node->attributes()  // instanceof \SimpleXMLElement
$b = $node->children() // instanceof \SimpleXMLElement

$a->asXML() // works
$a->count() // error
$a->getName() // does not work
// all the other method call will trigger the error

Test script:

$xml = simplexml_load_string('<root/>');
var_dump($xml->NOT_EXISTING_NODE_NAME instanceof \SimpleXMLElement);

$attributes = $xml->NOT_EXISTING_NODE_NAME->attributes();

Expected result:
PHP Warning:  count(): Node no longer exists in /home/goetas/test.php on line 7

Actual result:
PHP Warning:  count(): Node no longer exists in /home/goetas/test.php on line 7


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-07 13:06 UTC]
-Status: Open +Status: Feedback
 [2017-09-07 13:06 UTC]
According to this
you can use those methods on a non-existent child without warnings, but if you try them on attributes() or children() then you will get warnings.

Which makes sense to me: accessing a missing node and trying basic functions on it should be safe, but continuing on to using its attributes or children after you disregarded the opportunity to check for errors (ie, if the node is missing) should create warnings.

Anyway, are you asking to add warnings to the child or to remove warnings from the atttributes/children?
 [2017-09-07 13:20 UTC] goetas at gmail dot com
-Status: Feedback +Status: Open
 [2017-09-07 13:20 UTC] goetas at gmail dot com
Im asking to remove the warnings or to add some method to understand that the node does not exists (IMO should be done both)

There are more people online had similar issues.... 

I have a function that receives as argument an simplexmlelement instance and there is not way to avoid a notice being triggered.

More specifically, if you take a look at this function ( there is no way to understand if an notice is going to be triggered or not...
 [2017-09-07 13:23 UTC] goetas at gmail dot com
HHVM behavior in this case looks correct IMO
 [2017-09-28 15:08 UTC] goetas at gmail dot com
Currently using if ($value->getName() === "") to understand if I'm on an not-existing node
 [2018-08-06 22:57 UTC]
In my opinion, letting ::attributes() and ::children() return a
SimpleXMLElement has been an unfortunate design decision.  Instead
these methods should rather return an array-like object (say
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 16 10:01:28 2024 UTC