php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51462 Extending DOM classes work only if copy of object present
Submitted: 2010-04-02 12:51 UTC Modified: 2010-04-02 14:36 UTC
From: pahan at hubbitus dot info Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.3.2 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: pahan at hubbitus dot info
New email:
PHP Version: OS:

 

 [2010-04-02 12:51 UTC] 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) { }

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-02 13:19 UTC] rrichards@php.net
-Status: Open +Status: Wont fix
 [2010-04-02 13:19 UTC] rrichards@php.net
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 14:36 UTC] pahan at hubbitus dot info
-Package: DOM XML related +Package: *General Issues
 [2010-04-02 14:36 UTC] pahan at hubbitus dot info
>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...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 14:01:30 2024 UTC