note 26501 added to function.domnode-append-child

From: Date: Fri, 01 Nov 2002 18:46:43 +0000
Subject: note 26501 added to function.domnode-append-child
Groups: php.notes 
Request: Send a blank email to php-notes+get-38883@lists.php.net to get a copy of this message
Be careful of the order you append childs. (PHP <= 4.2) If you create let's say: $x_html = $x_doc->create_element('html'); $x_head, $x_title, $x_meta, $x_link and $x_body in the same way. If you: $x_head->append_child($x_title); $x_head->append_child($x_meta); $x_head->append_child($x_link); $x_html->append_child($x_head); $x_body_backup = $x_html->append_child($x_body); /* here */ $x_doc->append_child($x_html); If you now try to modify $x_body_backup.. it will no longer be able to modify the actualy XML tree, because you appended the HTML tree to the DomDocument.. and it has copied ALL the nodes at the end of the DomDocument, so the reference you now have in $x_body_backup is not valid anymore! If you had modified it where the /* here */ line is.. the $x_body_backup would still be valid.. but not after the $x_doc->append_child($x_html); What you can do is, reverse the order of your appendings.. so that you append the highest level node at the end.. so that your reference is still valid after, or wait till you have filled all of the nodes you wanted to add before you append it to your parent node. -- http://www.php.net/manual/en/function.domnode-append-child.php http://master.php.net/manage/user-notes.php?action=edit+26501 http://master.php.net/manage/user-notes.php?action=delete+26501 http://master.php.net/manage/user-notes.php?action=reject+26501

« previous php.notes (#38883) next »