note 85874 added to book.dom

From: Date: Mon, 22 Sep 2008 13:19:27 +0000
Subject: note 85874 added to book.dom
Groups: php.notes 
Request: Send a blank email to php-notes+get-144609@lists.php.net to get a copy of this message
After searching how to extend the DOMDocument and DOMElement I found a way in the bug: http://bugs.php.net/bug.php?id=35104. The following code shows how: <?php class extDOMDocument extends DOMDocument { public function createElement($name, $value=null) { $orphan = new extDOMElement($name, $value); // new sub-class object $docFragment = $this->createDocumentFragment(); // lightweight container maintains "ownerDocument" $docFragment->appendChild($orphan); // attach $ret = $docFragment->removeChild($orphan); // remove return $ret; // ownerDocument set; won't be destroyed on method exit } // .. more class definition } class extDOMElement extends DOMElement { function __construct($name, $value='', $namespaceURI=null) { parent::__construct($name, $value, $namespaceURI); } // ... more class definition here } $doc = new extDOMDocument('test'); $el = $doc->createElement('tagname'); $el->setAttribute("attr", "val"); $doc->appendChild($el); // append discards the DOMDocumentFragment and just adds its child nodes, but ownerDocument is maintained. echo get_class($el)."<br/>"; echo get_class($doc->documentElement)."<br/>"; echo "<xmp>".$doc->saveXML()."</xmp>"; ?> ---- Server IP: 80.64.47.13 Probable Submitter: 77.209.91.149 ---- Manual Page -- http://www.php.net/manual/en/book.dom.php Edit -- https://master.php.net/note/edit/85874 Del: integrated -- https://master.php.net/note/delete/85874/integrated Del: useless -- https://master.php.net/note/delete/85874/useless Del: bad code -- https://master.php.net/note/delete/85874/bad+code Del: spam -- https://master.php.net/note/delete/85874/spam Del: non-english -- https://master.php.net/note/delete/85874/non-english Del: in docs -- https://master.php.net/note/delete/85874/in+docs Del: other reasons-- https://master.php.net/note/delete/85874 Reject -- https://master.php.net/note/reject/85874 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144609) next »