Edit report at http://bugs.php.net/bug.php?id=51462&edit=1
ID: 51462
User updated by: pahan at hubbitus dot info
Reported by: pahan at hubbitus dot info
Summary: Extending DOM classes work only if copy of object
present
Status: Wont fix
Type: Bug
-Package: DOM XML related
+Package: *General Issues
Operating System: Linux
PHP Version: 5.3.2
New Comment:
>The tree does not maintain object state.
Who does it?
> If you are only looking for your own
> classtypes to be returned by the DOM methods/properties then use
> registerNodeClass() to register the types.
I do not speak about DOMDocument createElement method return my class
element. I
only report what element class lost after add it into tree! And in any
case BUG
what it behaves differently dependent to outer links present:
1) If it uses reference to provided object, why it unexpectedly change
type of
it?
2) If it copy object, why it depend on links??
In any case, when method relied on foreign variables it is not
determinated! And
it should be fixed in some way...
Previous Comments:
------------------------------------------------------------------------
[2010-04-02 13:19:32] [email protected]
The tree does not maintain object state. If you are only looking for
your own
classtypes to be returned by the DOM methods/properties then use
registerNodeClass() to register the types.
------------------------------------------------------------------------
[2010-04-02 12:51:04] pahan at hubbitus dot info
Description:
------------
Extending some DOM classes (f.e. DOMDOcument, DOMElement I think others,
but not
tested) work. But when such elemenmt appended to the document,
additional
information lost. Meantime, when copy of object saved alsewere, all work
as
expected.
First mention of this bug and initial workaround in comment
http://php.net/manual/tr/book.dom.php#80613 by ryoroxdahouse at hotmail
dot com.
I make automattical workaround like (
http://hinikato.blogspot.com/2009/10/domelement.html?
showComment=1270202266600#c284093039037001302 ):
class MyElement extends DOMElement
{
public function __construct($name, $value = null, $uri = null) {
parent::__construct($name, $value, $uri);
$GLOBALS['PHP_HACK_DOM_ELEMENT'][spl_object_hash($this)] = $this;
//Note,
reference is not enough :(
}
public function __destruct() {
unset($GLOBALS['PHP_HACK_DOM_ELEMENT'][spl_object_hash($this)]);
}
}
But as we need object copy, not just reference it requires double of
memmory!
Test script:
---------------
<?
class MyElement extends DOMElement{}
$doc = new DOMDocument();
$doc->appendChild(new MyElement('test'));
var_dump($doc->documentElement);
?>
Expected result:
----------------
object(MyElement)#4 (0) { }
Actual result:
--------------
object(DOMElement)#4 (0) { }
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51462&edit=1