|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #53890 DOMDocumentFragment and AppendChild
Submitted: 2011-01-31 12:21 UTC Modified: 2011-02-11 14:19 UTC
From: giosh94mhz at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2011-01-31 12:21 UTC] giosh94mhz at gmail dot com
From manual page:
Undocumented behaviour for appendChild using a DOMDocumentFragment

Test script:
$doc=new DOMDocument();

$root=$doc->appendChild( $doc->createElement('root') );

$frag->appendChild( $doc->createElement('fragNode') );

echo $frag->childNodes->length;


echo $frag->childNodes->length;

Expected result:
Documented behaviour.

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-10 13:09 UTC]
-Status: Open +Status: Bogus
 [2011-02-10 13:09 UTC]
Works as expected. The appendChild() method actually moves the node if it is already somewhere else.
 [2011-02-11 14:19 UTC] giosh94mhz at gmail dot com
I know that appending a node to another node change parentNode reference.

Anyway, DOMDocumentFragment is a "special" node; using


doesn't change the parent of fragment, but the parent of all the child nodes, so it is equivalent to:

foreach( $fragment->childNodes as $child )
     $parent->appendChild( $child );

So, "$fragment->childNodes->length" change after appending the fragment. This may seem obvious, but note that a DOMDocumentFragment is an instance of DOMNode: the user must be informed of that "unexpected" behaviour.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Jan 27 08:05:53 2023 UTC