PHP5 DOM extension - using firstChild

  • Follow


I have a script that uses xpath to retrieve an element using the PHP5
DOM extension and I want to delete all of the children of this
element.  I use hasChildNodes and firstChild to do this.  Here is my
script:

    $xpath = new DOMXPath($doc);
    $query = "//invitee[@id=\"$id\"]";
    $nodelist = $xpath->query($query);
    $hndChild = $nodelist->item(0);
    print get_class($hndChild);

    while($hndChild->hasChildNodes()) {
       print $hndChild->firstChild->nodeValue;
       $toGo = $hndChild->firstChild();          <-- failing statement
       $hndChild->removeChild($toGo);
     }

My get_class print verifies that I have a DOMElement.  I have a print
statement that prints the nodeValue of the firstChild as the first
statement of my while loop and I get the correct result.  But then my
next statement fails (on the first iteration) with this error message:

    Fatal error: Call to undefined method DOMElement::firstChild() in
save.php on line 51

I can't figure out why this second firstChild statement (failing
statement above) fails.  Seems like if firstChild is an undefined
method then the firstChild->nodeValue would also fail but it doesn't.
Can anyone enlighten me on what might be going on here?

Denis
0
Reply sysdrk (9) 4/8/2012 9:17:00 PM

On Sun, 08 Apr 2012 14:17:00 -0700, Denis wrote:

print $hndChild->firstChild->nodeValue; 

accesses the firstChild *property* of $hndChild

$toGo = $hndChild->firstChild();          <-- failing statement

accesses the firstChild() *method* of $hndChild

if firstChild a property or a method?

in other words, is it a member variable or a member function?

Maybe:

$toGo = $hndChild->firstChild;

would work? I'm just speculating, based on the slight difference in the 
two lines you indicate, "firstChild" vs "firstChild()".

Rgds

Denis McMahon
0
Reply denismfmcmahon (364) 4/8/2012 9:31:28 PM


On Apr 8, 4:31=A0pm, Denis McMahon <denismfmcma...@gmail.com> wrote:
> On Sun, 08 Apr 2012 14:17:00 -0700, Denis wrote:
>
> print $hndChild->firstChild->nodeValue;
>
> accesses the firstChild *property* of $hndChild
>
> $toGo =3D $hndChild->firstChild(); =A0 =A0 =A0 =A0 =A0<-- failing stateme=
nt
>
> accesses the firstChild() *method* of $hndChild
>
> if firstChild a property or a method?
>
> in other words, is it a member variable or a member function?
>
> Maybe:
>
> $toGo =3D $hndChild->firstChild;
>
> would work? I'm just speculating, based on the slight difference in the
> two lines you indicate, "firstChild" vs "firstChild()".
>
> Rgds
>
> Denis McMahon

You are absolutely right.  I don't know why I couldn't see that.  I
knew firstChild was a property but this just never registered in my
mind.  I made the change to a property reference and it works.  Thanks
a bunch.

Denis
0
Reply sysdrk (9) 4/8/2012 9:58:23 PM

2 Replies
17 Views

(page loaded in 0.063 seconds)


Reply: