[PHP] Re: Extending DOMNode
Eli wrote: ?php class MyDOMNode extends DOMNode { public $v = 10; function __construct() {} } $dom = new DOMDocument(); $dom-registerNodeClass('DOMNode','MyDOMNode'); $dom-loadXML('roota//root'); echo $dom-firstChild-v; #-- not outputs 10 ? But I get the notice: PHP Notice: Undefined property: DOMElement::$v in ... I want the extension to be valid for all DOM nodes that are derived from DOMNode, such as DOMElement, DOMAttr, DOMNodeList, DOMText, etc... I try not to extend all the classes one by one. Due to the internals of the DOM extension, you need to register the class types that are actually instantiated and not the underlying base DOMNode class. Unfortunately in your case this means you need to register all of those classes separately. $dom-registerNodeClass('DOMElement','MyDOMNode'); $dom-registerNodeClass('DOMAttr','MyDOMNode'); $dom-registerNodeClass('DOMText','MyDOMNode'); ... Rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Extending DOMNode
Rob Richards wrote: Due to the internals of the DOM extension, you need to register the class types that are actually instantiated and not the underlying base DOMNode class. Unfortunately in your case this means you need to register all of those classes separately. $dom-registerNodeClass('DOMElement','MyDOMNode'); $dom-registerNodeClass('DOMAttr','MyDOMNode'); $dom-registerNodeClass('DOMText','MyDOMNode'); ... Not good... :-( ?php class MyDOMNode extends DOMNode { public $x = 100; } $dom = new DOMDocument(); $dom-registerNodeClass('DOMElement','MyDOMNode'); ? PHP Fatal error: DOMDocument::registerNodeClass(): Class MyDOMNode is not derived from DOMElement. So I have to extend DOMElement and register MyDOMElement. But all my nodes should be also based on MyDOMNode. Problem is that in PHP you can only extend one class in a time, so you cannot build your own class-tree which extends a base class-tree of DOM. :-/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Extending DOMNode
Eli wrote: Rob Richards wrote: Due to the internals of the DOM extension, you need to register the class types that are actually instantiated and not the underlying base DOMNode class. Unfortunately in your case this means you need to register all of those classes separately. $dom-registerNodeClass('DOMElement','MyDOMNode'); $dom-registerNodeClass('DOMAttr','MyDOMNode'); $dom-registerNodeClass('DOMText','MyDOMNode'); ... Not good... :-( ?php class MyDOMNode extends DOMNode { public $x = 100; } $dom = new DOMDocument(); $dom-registerNodeClass('DOMElement','MyDOMNode'); ? PHP Fatal error: DOMDocument::registerNodeClass(): Class MyDOMNode is not derived from DOMElement. So I have to extend DOMElement and register MyDOMElement. But all my nodes should be also based on MyDOMNode. Problem is that in PHP you can only extend one class in a time, so you cannot build your own class-tree which extends a base class-tree of DOM. :-/ maybe the runkit extension can help - no idea how big it might explode in your face if you try to hack the DOM* stuff with runkit :-) you never stated why you want to extend all the DOM classes, maybe there is a different way of achieving what you want (i.e. without going through the hassle of what you seem to have to do at the moment) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Extending DOMNode
Jochem Maas wrote: maybe the runkit extension can help - no idea how big it might explode in your face if you try to hack the DOM* stuff with runkit :-) you never stated why you want to extend all the DOM classes, maybe there is a different way of achieving what you want (i.e. without going through the hassle of what you seem to have to do at the moment) I want to add a common function to all nodes extended from DOMNode (e.g DOMElement, DOMText, DOMAttr, etc), and also keep the code maintainable so changing the common function will not force me to do in 10 places [I don't want to create a global function for this]. -thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Extending DOMNode
Eli wrote: Jochem Maas wrote: maybe the runkit extension can help - no idea how big it might explode in your face if you try to hack the DOM* stuff with runkit :-) you never stated why you want to extend all the DOM classes, maybe there is a different way of achieving what you want (i.e. without going through the hassle of what you seem to have to do at the moment) I want to add a common function to all nodes extended from DOMNode (e.g DOMElement, DOMText, DOMAttr, etc), and also keep the code maintainable so changing the common function will not force me to do in 10 places why? [I don't want to create a global function for this]. why not? -thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Extending DOMNode
Eli wrote: Rob Richards wrote: Due to the internals of the DOM extension, you need to register the class types that are actually instantiated and not the underlying base DOMNode class. Unfortunately in your case this means you need to register all of those classes separately. $dom-registerNodeClass('DOMElement','MyDOMNode'); $dom-registerNodeClass('DOMAttr','MyDOMNode'); $dom-registerNodeClass('DOMText','MyDOMNode'); ... Not good... :-( ?php class MyDOMNode extends DOMNode { public $x = 100; } $dom = new DOMDocument(); $dom-registerNodeClass('DOMElement','MyDOMNode'); ? PHP Fatal error: DOMDocument::registerNodeClass(): Class MyDOMNode is not derived from DOMElement. So I have to extend DOMElement and register MyDOMElement. But all my nodes should be also based on MyDOMNode. Problem is that in PHP you can only extend one class in a time, so you cannot build your own class-tree which extends a base class-tree of DOM. :-/ Ooops. Wasn't thinking when I wrote that. Yup, you will need to extend every one of the node types separately and then register those classes. Rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php