|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-08-23 15:10 UTC] php at maisqi dot com
Description:
------------
When we invoke a non existent sub-node, SimpleXMLElement returns a SimpleXMLElement object. But when we try to get a non existent sub-node on that object, it returns NULL.
IMO this renderes it inconsistent.
Test script:
---------------
<?php
class XXX {
public function test () {
$sx = new \SimpleXMLElement ('<x></x>');
$node = $sx->node;
echo '1: $node = $sx->node => ', $node === $this ? 'this' : gettype ($node) . ':' . get_class ($node), ' node->count: ', $node->count (), "<br />\n";
$node = $sx->node->node;
echo '2: $node = $node->node => ', $node === $this ? 'this' : gettype ($node), "<br />\n";
}
}
$x = new XXX;
$x->test ();
Expected result:
----------------
Or both ($sx and $node) SimpleXMLElement objects should return NULL for a non existent sub-node or both sould return a SimpleXMLElement object. As it is, it's a surprise lurking.
I think the second option is best. It makes things like this possible:
if ($products = $sx->Catalog->Products->Product) {
// Never gets here if there's no <Catalog>, or if it has no sub-nodes
// named <Products>, or if it has no sub-nodes named Product.
}
else die ('No products');
Actual result:
--------------
$node it's a SimpleXMLElement, then it is NULL.
1: $node = $sx->node => object:SimpleXMLElement node->count: 0<br />
2: $node = $node->node => NULL<br />
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 14:00:01 2025 UTC |
A simpler test script: <?php $sx = new \SimpleXMLElement ('<x></x>'); $node = $sx->node; echo '1: $node = $sx->node => ', get_class ($node), "<br />\n"; $node = $node->node; echo '2: $node = $node->node => ', gettype ($node), "<br />\n";