note 103151 added to class.domdocumentfragment

From: Date: Tue, 29 Mar 2011 11:06:26 +0000
Subject: note 103151 added to class.domdocumentfragment
Groups: php.notes 
Request: Send a blank email to php-notes+get-178290@lists.php.net to get a copy of this message
Note that DOMDocumentFragment is a bit special when it's added to another node. When that happens, not the fragment itself is added as a child, but all of the children of the fragment are moved over to the new parent node. For example, consider this script: <?php /* Create a document and a fragment containing a single node */ $doc = new DOMDocument(); $fragment = $doc->createDocumentFragment(); $fragment->appendChild($doc->createElement('foo')); /* Now, the foo node is a child of the fragment */ var_dump($fragment->firstChild); /* After appending the fragment to another node, the children of the * fragment will have been transfered to that node (and the fragment is * not present in the children list!) */ $doc->appendChild($fragment); /* So the fragment has no children anymore */ var_dump($fragment->firstChild); /* But $doc has a single child, which is the foo element, not the * fragment */ var_dump($doc->childNodes->length); var_dump($doc->firstChild); ?> This produces the following output: object(DOMElement)#3 (0) { } NULL int(1) object(DOMElement)#3 (0) { } ---- Server IP: 69.147.83.197 Probable Submitter: 82.169.104.215 ---- Manual Page -- http://www.php.net/manual/en/class.domdocumentfragment.php Edit -- https://master.php.net/note/edit/103151 Del: integrated -- https://master.php.net/note/delete/103151/integrated Del: useless -- https://master.php.net/note/delete/103151/useless Del: bad code -- https://master.php.net/note/delete/103151/bad+code Del: spam -- https://master.php.net/note/delete/103151/spam Del: non-english -- https://master.php.net/note/delete/103151/non-english Del: in docs -- https://master.php.net/note/delete/103151/in+docs Del: other reasons-- https://master.php.net/note/delete/103151 Reject -- https://master.php.net/note/reject/103151 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178290) next »