Sometimes you may think: Wouldn’t it be cool if I can extend DOMNode and since all other functions extend DOMNode, my custon functions will be available in every element?
The bad news, it will not work for DOMNode, but the other elements which extend from it.
Why is that?
PHP offers a special function, which can be used to extend the DOM, its called registerNodeClass and is located in DOMDocument.
1 |
bool DOMDocument::registerNodeClass ( string $baseclass , string $extendedclass ) |
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.
Here is the example from PHP.net:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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(); |
If you want to replace a function with your own, your function has to extend the function you want to replace.