note 80613 added to ref.dom

From: Date: Fri, 25 Jan 2008 08:14:26 +0000
Subject: note 80613 added to ref.dom
Groups: php.notes 
Request: Send a blank email to php-notes+get-135393@lists.php.net to get a copy of this message
I use DOM to generate dynamically XHTML document. When trying to extend the DOMDocument and DOMElement classes, I found a very annoying bug concerning DOMDocument::$documentElement. If you create your own custom element extending DOMElement and append him in place of the document element, you cannot access to any new members newly defined in your custom class via DOMDocument::$documentElement. In my situation, I cannot use DOMDocument::registerNodeClass() because the document element is not necessarily the base class for all the elements in my document. ******* problem ******* See bellow for the repro step: <?php class MyElement extends DOMElement{ public $myProp="myProp"; public function myMethod(){ return 'myMethod()'; } } $myDocument=new DOMDocument(); $myDocument->appendChild(new MyElement('myElement','myElement')); echo ('$myElement->myProp :'.$myDocument->documentElement->myProp.'<br />'); echo ('$myElement->myMethod :'.$myDocument->documentElement->myMethod().'<br />'); ?> will output: Notice: Undefined property: DOMElement::$myProp in C:\Program Files\EasyPHP 2.0b1\www\testDOMBug\test2.php on line 11 $myElement->myProp : Fatal error: Call to undefined method DOMElement::myMethod() in C:\Program Files\EasyPHP 2.0b1\www\testDOMBug\test2.php on line 12 ******* solution ******* After searching around, I found a pretty odd way to fix this problem. It seems that you have to stock a reference to your appended document element in an user-defined (and persistent) variable (in other words, not only in DOMDocument::$documentElement). See below: <?php class MyElement extends DOMElement{ public $myProp="myProp"; public function myMethod(){ return 'myMethod()'; } } $myDocument=new DOMDocument(); $mydocumentElement=$myDocument->appendChild(new MyElement('myElement','myElement')); //here is the hack echo ('$myElement->myProp :'.$myDocument->documentElement->myProp.'<br />'); echo ('$myElement->myMethod :'.$myDocument->documentElement->myMethod().'<br />'); ?> will output: $myElement->myProp :myProp $myElement->myMethod :myMethod() Hope it will help. ---- Server IP: 69.147.83.197 Probable Submitter: 207.46.92.19 ---- Manual Page -- http://www.php.net/manual/en/ref.dom.php Edit -- https://master.php.net/note/edit/80613 Del: integrated -- https://master.php.net/note/delete/80613/integrated Del: useless -- https://master.php.net/note/delete/80613/useless Del: bad code -- https://master.php.net/note/delete/80613/bad+code Del: spam -- https://master.php.net/note/delete/80613/spam Del: non-english -- https://master.php.net/note/delete/80613/non-english Del: in docs -- https://master.php.net/note/delete/80613/in+docs Del: other reasons-- https://master.php.net/note/delete/80613 Reject -- https://master.php.net/note/reject/80613 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135393) next »