Bug #75168 [Com]: Unexpected PHP Warning Node no longer exists
| From: | goetas at gmail dot com | Date: | Thu, 28 Sep 2017 15:08:30 +0000 |
| Subject: | Bug #75168 [Com]: Unexpected PHP Warning Node no longer exists | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-211423@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75168&edit=1
ID: 75168
Comment by: goetas at gmail dot com
Reported by: goetas at gmail dot com
Summary: Unexpected PHP Warning Node no longer exists
Status: Open
Type: Bug
Package: SimpleXML related
Operating System: any
PHP Version: 7.0.23
Block user comment: N
Private report: N
New Comment:
Currently using if ($value->getName() === "") to understand if I'm on an
not-existing node
Previous Comments:
------------------------------------------------------------------------
[2017-09-07 13:23:21] goetas at gmail dot com
HHVM behavior in this case looks correct IMO
------------------------------------------------------------------------
[2017-09-07 13:20:55] 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:06:19] 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 12:33:49] 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)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75168&edit=1