php.net |  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
Votes:9
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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: goetas at gmail dot com
New email:
PHP Version: OS:

 

 [2017-09-07 12:33 UTC] goetas at gmail dot com
Description:
------------
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:
---------------
<?php

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

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

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


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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-07 13:06 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2017-09-07 13:06 UTC] requinix@php.net
According to this
  https://3v4l.org/1BHKS
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.... https://stackoverflow.com/questions/2502038/simplexml-node-no-longer-exists 


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 (https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/XmlDeserializationVisitor.php#L398) 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] cmb@php.net
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
SimpleXMLElementList).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 13 18:01:28 2024 UTC