Doc #73252 [Opn]: DOMXPath: doc passed in constructor not always equals $xpath->document.

From: Date: Wed, 07 Jul 2021 17:14:22 +0000
Subject: Doc #73252 [Opn]: DOMXPath: doc passed in constructor not always equals $xpath->document.
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-18925@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73252&edit=1

 ID:                 73252
 Updated by:         cmb@php.net
 Reported by:        arjen at parse dot nl
 Summary:            DOMXPath: doc passed in constructor not always
                     equals $xpath->document.
 Status:             Open
 Type:               Documentation Problem
 Package:            DOM XML related
 Operating System:   Linux
 PHP Version:        7.0.11
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2021-07-07 17:04:23] cmb@php.net

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.

------------------------------------------------------------------------
[2016-10-05 12:41:54] arjen at parse dot nl

Description:
------------
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.


See https://3v4l.org/XLInV

Test script:
---------------
<?php


class CustomDOMDocument extends DOMDocument
{
}

function localScope()
{
    $doc = new CustomDOMDocument;
    $doc->loadHtml('<p>Test123</p>');
    $xpath = new DOMXPath($doc);
    
    return $xpath;
}


$doc = new CustomDOMDocument;
$doc->loadHtml('<p>Test123</p>');
$xpath = new DOMXPath($doc);

var_dump(get_class($xpath->document));


$xpath = new DOMXPath(new CustomDOMDocument);
var_dump(get_class($xpath->document));

$xpath = localScope();
var_dump(get_class($xpath->document));

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

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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=73252&edit=1


Thread (2 messages)

« previous php.doc.bugs (#18925) next »