php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65535 Non existent properties should be regular SimpleXMLElement objects
Submitted: 2013-08-23 15:10 UTC Modified: 2013-08-23 15:14 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: php at maisqi dot com Assigned:
Status: Open Package: SimpleXML related
PHP Version: 5.4.19 OS: Probably all
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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 />

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-23 15:14 UTC] php at maisqi dot com
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";
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Sep 22 14:01:27 2019 UTC