Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:29717 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 26740 invoked by uid 1010); 24 May 2007 13:07:07 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 26725 invoked from network); 24 May 2007 13:07:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 May 2007 13:07:07 -0000 Authentication-Results: pb1.pair.com header.from=news@sea.gmane.org; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=php-php-dev@m.gmane.org; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain m.gmane.org designates 80.91.229.2 as permitted sender) X-PHP-List-Original-Sender: php-php-dev@m.gmane.org X-Host-Fingerprint: 80.91.229.2 main.gmane.org Linux 2.5 (sometimes 2.4) (4) Received: from [80.91.229.2] ([80.91.229.2:36902] helo=ciao.gmane.org) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 07/C4-32127-7FD85564 for ; Thu, 24 May 2007 09:07:05 -0400 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1HrD1o-0001hR-0J for internals@lists.php.net; Thu, 24 May 2007 15:06:52 +0200 Received: from blueice3n1.uk.ibm.com ([195.212.29.83]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 24 May 2007 15:06:51 +0200 Received: from cem by blueice3n1.uk.ibm.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 24 May 2007 15:06:51 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: internals@lists.php.net Date: Thu, 24 May 2007 14:06:42 +0100 Lines: 50 Message-ID: References: <261daaa10705220123m41b254a6p6b44d21b29ad1d4c@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: blueice3n1.uk.ibm.com User-Agent: Thunderbird 2.0.0.0 (Windows/20070326) In-Reply-To: <261daaa10705220123m41b254a6p6b44d21b29ad1d4c@mail.gmail.com> Sender: news Subject: Re: [PHP-DEV] unexpected behaviour in SimpleXML (php 5.2.2) From: cem@php.net (Caroline Maynard) Vesselin Kenashkov wrote: > > Here is the first one - how one can find does a SimpleXMLElement has child > nodes? > Here is an example: > -------- > $str = ''; > $x = new SimpleXMLElement($str); > > if($x->subnode->children()) > print 'yes'; > else > print 'no'; > --------- > will print 'no'; > > If the > $str=''; > it will print yes. > But the same will happen if the subnode has an attribute like: > $str = ''; > > 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. > > The workaround I use is to extend the SimpleXMLElement > -------- > class SimpleXMLElement2 extends SimpleXMLElement > { > public function has_children() > { > foreach($this->children() as $node) > return true; > return false; > } > > } > ------ > And then we can check with if($x->subnode->has_children()) That's how SimpleXML is, but you can ease your pain by using count(): $x = new SimpleXMLElement($str); if (count($x->subnode->children())) print 'yes'; else print 'no';