class myDOMNode extends DOMNode { public function foo() { echo "Hello World!"; } } $doc = new DOMDocument(); $doc->registerNodeClass('DOMNode', 'myDOMNode'); $root = $doc->createElement('root'); $root->foo(); // outputs "Hello World!"
class myDOMElement extends DOMElement { public function bar() { echo "Goodbye World!"; } } $doc = new DOMDocument(); $doc->registerNodeClass('DOMElement', 'myDOMElement'); $root = $doc->createElement('root'); $child = $doc->createElement('node-name'); $root->appendChild($child); $child->bar(); // outputs "Goodbye World!"In this example, when the DOM parser creates a new DOMElement with the name "node-name", it will instead instantiate a new myDOMElement object. We can then call the bar method on the child element, which was created using the custom node class. Package library used: The PHP DOMDocument class is part of the PHP core library and does not require any external package libraries.