Edit report at https://bugs.php.net/bug.php?id=65535&edit=1

 ID:                 65535
 User updated by:    php at maisqi dot com
 Reported by:        php at maisqi dot com
 Summary:            Non existent properties should be regular
                     SimpleXMLElement objects
 Status:             Open
 Type:               Bug
 Package:            SimpleXML related
 Operating System:   Probably all
 PHP Version:        5.4.19
 Block user comment: N
 Private report:     N

 New Comment:

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";


Previous Comments:
------------------------------------------------------------------------
[2013-08-23 15:10:50] 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 />


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65535&edit=1

Reply via email to