php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #26380 empty($SimpleXMLObject) doesn't return true when empty
Submitted: 2003-11-24 07:36 UTC Modified: 2003-11-26 17:07 UTC
From: bart at mediawave dot nl Assigned: helly (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 5 OS: *
Private report: No CVE-ID: None
 [2003-11-24 07:36 UTC] bart at mediawave dot nl
Description:
------------
This bug/feature request has some relation with bug: #25640. 

I've loaded XML into a simpleXML object. SimpleXML currently loads empty tags (e.g. <tag />) as empty SimpleXML objects.

With the SimpleXML object I wanted to use the following code to find out whether a tag has child tags or not:

if (is_object($SimpleXMLObjectNode)) {
   // $node has child tags

Unfortunately this doesn't work very well since this code will think that empty tags have child tags too. (Since empty tags are loaded as objects)

Therefore I thought it would be a nice feature to be able to find out whether an object is empty or not. Something like:

if (empty($object)) {
   // Object is empty
}

Reproduce code:
---------------
<?php
$xml = '<wrapper><foo></foo><bar>s2</bar><bar>s3</bar></wrapper>';
$t = simplexml_load_string($xml);
print_r($t);
if (empty($t->foo)) {
	echo 'Tag is empty';
} else {
	echo 'Tag has contents';
}
?>

Expected result:
----------------
Tag is empty

Actual result:
--------------
Tag has contents

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-24 07:56 UTC] bart at mediawave dot nl
Changed this to "Zend Engine 2 problem" because it seems empty() should return true if an object has no properties:

http://www.php.net/manual/en/function.empty.php
 [2003-11-25 14:50 UTC] sniper@php.net
The object is not empty. No bug here.

 [2003-11-26 09:29 UTC] bart at mediawave dot nl
simplexml_element Object
(
    [foo] => simplexml_element Object
        (
        )

    [bar] => Array
        (
            [0] => s2
            [1] => s3
        )

)

[foo] looks empty to me? 

Or maybe there are private properties that I can't see? If so, should empty() return true if an object only has private properties?

How else can we tell if an SimpleXML object represents an empty tag?
 [2003-11-26 17:07 UTC] helly@php.net
Please leave this bug closed!
empty() has a different meaning than the one you seem to expect.
Objects can never be empty!
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 08 16:01:33 2025 UTC