#41662 [NEW]: SimpleXML->children() misleading result

From: Date: Tue, 12 Jun 2007 07:36:50 +0000
Subject: #41662 [NEW]: SimpleXML->children() misleading result
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-113673@lists.php.net to get a copy of this message
From: kenashkov at gmail dot com Operating system: Fedora Core 4 PHP version: 5.2.3 PHP Bug Type: SimpleXML related Bug description: SimpleXML->children() misleading result Description: ------------ Testing an SimpleXMLElement object for children is unconsistent. Here is an example: -------- $str = '<rootnode><subnode></subnode></rootnode>'; $x = new SimpleXMLElement($str); if($x->subnode->children()) print 'yes'; else print 'no'; --------- will print 'no'; If the $str='<rootnode><subnode><newnode></newnode></subnode></rootnode>'; it will print yes. But the same will happen if the subnode has an attribute like: $str = '<rootnode><subnode id="2"></subnode></rootnode>'; But if we use foreach($x->subnode->children() as $key=>$value) in the latter example we will not get anything (which is correct). I think is wrong the children() method to return object when there are no child objects and the node has attributes (because for querying the attributes we have the attributes() method). This was discussed in php-dev list - http://marc.info/?l=php-dev&m=118001203709813&w=2 If this is not a bug, I think a note regarding this behaviour must be added in the docs. Reproduce code: --------------- $str = '<rootnode><subnode id="2"></subnode></rootnode>'; $x = new SimpleXMLElement($str); if($x->subnode->children()) print 'yes'; else print 'no'; Expected result: ---------------- no Actual result: -------------- yes -- Edit bug report at http://bugs.php.net/?id=41662&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=41662&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=41662&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=41662&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=41662&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=41662&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=41662&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=41662&r=needscript Try newer version: http://bugs.php.net/fix.php?id=41662&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=41662&r=support Expected behavior: http://bugs.php.net/fix.php?id=41662&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=41662&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=41662&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=41662&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=41662&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=41662&r=dst IIS Stability: http://bugs.php.net/fix.php?id=41662&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=41662&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=41662&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=41662&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=41662&r=mysqlcfg

« previous php.bugs (#113673) next »