|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #73252 DOMXPath: doc passed in constructor not always equals $xpath->document.
Submitted: 2016-10-05 12:41 UTC Modified: 2021-07-07 17:14 UTC
From: arjen at parse dot nl Assigned:
Status: Open Package: DOM XML related
PHP Version: 7.0.11 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.
Block user comment
Status: Assign to:
Bug Type:
From: arjen at parse dot nl
New email:
PHP Version: OS:


 [2016-10-05 12:41 UTC] arjen at parse dot nl
When passing an object CustomDOMDocument extending DOMDocument as the DOMXPath object, the document property of DOMXpath is of type CustomDOMDocument.

As soon as the original document is destructed (because of scope), a clone of the object is stored internally, but with type DOMDocument and not the CustomDOMDocument class.


Test script:

class CustomDOMDocument extends DOMDocument

function localScope()
    $doc = new CustomDOMDocument;
    $xpath = new DOMXPath($doc);
    return $xpath;

$doc = new CustomDOMDocument;
$xpath = new DOMXPath($doc);


$xpath = new DOMXPath(new CustomDOMDocument);

$xpath = localScope();

Expected result:
string(17) "CustomDOMDocument"
string(17) "CustomDOMDocument"
string(17) "CustomDOMDocument"

Actual result:
string(17) "CustomDOMDocument"
string(11) "DOMDocument"
string(11) "DOMDocument"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-07 17:04 UTC]
-Type: Bug +Type: Documentation Problem
 [2021-07-07 17:04 UTC]
That is because all DOMNode objects are transient, i.e. after they
have been destroyed, they will be rebuilt from the libxml2 DOM
nodes, loosing all customizations.  There is not much we can do
about that, besides documenting this issue, and tell users that
they must prevent these instances from being destroyed.
 [2021-07-07 17:14 UTC]
Ah, that's not quite true.  If you ::registerNodeClass(), you will
get an instance of that class back, but still custom properties
are lost, since the instance is re-constructed.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun Dec 03 21:01:29 2023 UTC