Php/docs/domdocument.registernodeclass

来自菜鸟教程
跳转至:导航、​搜索

DOMDocument::registerNodeClass

(PHP 5 >= 5.2.0, PHP 7)

DOMDocument::registerNodeClassRegister extended class used to create base node type


说明

public DOMDocument::registerNodeClass ( string $baseclass , string $extendedclass ) : bool

This method allows you to register your own extended DOM class to be used afterward by the PHP DOM extension.

This method is not part of the DOM standard.


参数

baseclass
The DOM class that you want to extend. You can find a list of these classes in the chapter introduction.
extendedclass
Your extended class name. If null is provided, any previously registered class extending baseclass will be removed.


返回值

成功时返回 true, 或者在失败时返回 false


范例

Example #1 Adding a new method to DOMElement to ease our code

<?phpclass myElement extends DOMElement {   function appendElement($name) {       return $this->appendChild(new myElement($name));   }}class myDocument extends DOMDocument {   function setRoot($name) {       return $this->appendChild(new myElement($name));   }}$doc = new myDocument();$doc->registerNodeClass('DOMElement', 'myElement');// From now on, adding an element to another costs only one method call ! $root = $doc->setRoot('root');$child = $root->appendElement('child');$child->setAttribute('foo', 'bar');echo $doc->saveXML();?>

以上例程会输出:


<?xml version="1.0"?>
<root><child foo="bar"/></root>

Example #2 Retrieving elements as custom class

<?phpclass myElement extends DOMElement {    public function __toString() {        return $this->nodeValue;    }}$doc = new DOMDocument;$doc->loadXML("<root><element><child>text in child</child></element></root>");$doc->registerNodeClass("DOMElement", "myElement");$element = $doc->getElementsByTagName("child")->item(0);var_dump(get_class($element));// And take advantage of the __toString method..echo $element;?>

以上例程会输出:


string(9) "myElement"
text in child

Example #3 Retrieving owner document

When instantiating a custom DOMDocument the ownerDocument property will refer to the instantiated class, meaning there is no need (and in fact not possible) to use DOMDocument::registerNodeClass() with DOMDocument


<?phpclass myDOMDocument extends DOMDocument {}class myOtherDOMDocument extends DOMDocument {}// Create myDOMDocument with some XML$doc = new myDOMDocument;$doc->loadXML("<root><element><child>text in child</child></element></root>");$child = $doc->getElementsByTagName("child")->item(0);// The current owner of the node is myDOMDocumentvar_dump(get_class($child->ownerDocument));// Import a node from myDOMDocument$newdoc = new myOtherDOMDocument;$child = $newdoc->importNode($child);// The new owner of the node has changed to myOtherDOMDocumentvar_dump(get_class($child->ownerDocument));?>

以上例程会输出:


string(13) "myDOMDocument"
string(18) "myOtherDOMDocument"

Example #4 Custom objects are transient

Caution Objects of the registered node classes are transient, i.e. they are destroyed when they are no longer referenced from PHP code, and recreated when being retrieved again. That implies that custom property values will be lost after recreation.


<?phpclass MyDOMElement extends DOMElement{    public $myProp = 'default value';}$doc = new DOMDocument();$doc->registerNodeClass('DOMElement', 'MyDOMElement');$node = $doc->createElement('a');$node->myProp = 'modified value';$doc->appendChild($node);echo $doc->childNodes[0]->myProp, PHP_EOL;unset($node);echo $doc->childNodes[0]->myProp, PHP_EOL;?>

以上例程会输出:


modified value
default value