<?php
/**
* The only reliable way of determining if a child exists
* in SimpleXMLElement is to use count(). All other methods
* do not work reliably in global or local NS.
*/
if(!class_exists('SimpleXMLElement')) die("Bonkers");
$xml = new SimpleXMLElement('<xml xmlns:abstract="urn:my.org:abstract"><abstract:node>foo</abstract:node><bar>123</bar></xml>');
var_dump($xml->node);
var_dump(gettype($xml->node));
var_dump(isset($xml->node));
var_dump(empty($xml->node));
var_dump(is_null($xml->node));
var_dump(count($xml->node));
var_dump(count($xml->node->children()));
echo "----------------------\n";
var_dump(gettype($xml->bar));
var_dump(isset($xml->bar));
var_dump(empty($xml->bar));
var_dump(is_null($xml->bar));
var_dump(count($xml->bar));
var_dump(count($xml->bar->children()));
echo "----------------------\n";
var_dump(gettype($xml->children('abstract',true)->node));
var_dump(isset($xml->children('abstract',true)->node));
var_dump(empty($xml->children('abstract',true)->node));
var_dump(is_null($xml->children('abstract',true)->node));
var_dump(count($xml->children('abstract',true)->node));
var_dump(count($xml->children('abstract',true)->node->children()));
echo "----------------------\n";
var_dump(gettype($xml->children('abstract',true)->bar));
var_dump(isset($xml->children('abstract',true)->bar));
var_dump(empty($xml->children('abstract',true)->bar));
var_dump(is_null($xml->children('abstract',true)->bar));
var_dump(count($xml->children('abstract',true)->bar));
var_dump(count($xml->children('abstract',true)->bar->children()));
object(SimpleXMLElement)#2 (0) {
}
string(6) "object"
bool(false)
bool(true)
bool(false)
int(0)
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in /in/GFCY2:16
Stack trace:
#0 {main}
thrown in /in/GFCY2 on line 16
Process exited with code 255.
Output for 8.0.0 - 8.0.9
object(SimpleXMLElement)#2 (0) {
}
string(6) "object"
bool(false)
bool(true)
bool(false)
int(0)
Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, null given in /in/GFCY2:16
Stack trace:
#0 {main}
thrown in /in/GFCY2 on line 16
Process exited with code 255.
object(SimpleXMLElement)#2 (0) {
}
string(6) "object"
bool(true)
bool(false)
bool(false)
int(0)
Warning: count(): Node no longer exists in /in/GFCY2 on line 16
int(0)
----------------------
string(6) "object"
bool(true)
bool(false)
bool(false)
int(1)
int(0)
----------------------
string(6) "object"
bool(true)
bool(false)
bool(false)
int(1)
int(0)
----------------------
string(6) "object"
bool(true)
bool(false)
bool(false)
int(0)
Warning: count(): Node no longer exists in /in/GFCY2 on line 37
int(0)
Output for 5.1.2 - 5.1.6
object(SimpleXMLElement)#2 (0) {
}
string(6) "object"
bool(true)
bool(false)
bool(false)
int(0)
Warning: count(): Node no longer exists in /in/GFCY2 on line 16
int(0)
----------------------
string(6) "object"
bool(true)
bool(false)
bool(false)
int(1)
int(0)
----------------------
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 25
Notice: Trying to get property of non-object in /in/GFCY2 on line 25
string(4) "NULL"
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 26
bool(false)
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 27
bool(true)
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 28
Notice: Trying to get property of non-object in /in/GFCY2 on line 28
bool(true)
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 29
Notice: Trying to get property of non-object in /in/GFCY2 on line 29
int(0)
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 30
Notice: Trying to get property of non-object in /in/GFCY2 on line 30
Fatal error: Call to a member function children() on a non-object in /in/GFCY2 on line 30
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.1
object(SimpleXMLElement)#2 (0) {
}
string(6) "object"
bool(true)
bool(false)
bool(false)
int(1)
int(1)
----------------------
string(6) "object"
bool(true)
bool(false)
bool(false)
int(1)
int(1)
----------------------
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 25
Notice: Trying to get property of non-object in /in/GFCY2 on line 25
string(4) "NULL"
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 26
bool(false)
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 27
bool(true)
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 28
Notice: Trying to get property of non-object in /in/GFCY2 on line 28
bool(true)
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 29
Notice: Trying to get property of non-object in /in/GFCY2 on line 29
int(0)
Warning: SimpleXMLElement::children() expects at most 1 parameter, 2 given in /in/GFCY2 on line 30
Notice: Trying to get property of non-object in /in/GFCY2 on line 30
Fatal error: Call to a member function children() on a non-object in /in/GFCY2 on line 30
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /in/GFCY2 on line 25
Process exited with code 255.