php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67459 DOMDocument memory hanldling
Submitted: 2014-06-17 06:45 UTC Modified: 2019-09-23 06:15 UTC
From: goetas at gmail dot com Assigned: beberlei (profile)
Status: Not a bug Package: DOM XML related
PHP Version: 5.5.13 OS: ubuntu
Private report: No CVE-ID: None
 [2014-06-17 06:45 UTC] goetas at gmail dot com
Description:
------------
Strange object allocation issue, maybe related to PHP/LIBXML memory handling.

I've supposed that PHP releases the object associated to $root when I unset it (even if this object can be reached through $dom->documentElement).

Later, when I create the $ele element, it takes the same memory position of $root.


LIBXML version 2.7.8

Test script:
---------------
$dom = new \DOMDocument('1.0', 'UTF-8');
$root = $dom->createElement('root');
$dom->appendChild($root);

$h1 = spl_object_hash($root);
$h2 = spl_object_hash($dom->documentElement);

var_dump($root === $dom->documentElement); // true, OK
var_dump($h1 === $h2); // true, OK

unset($root);

$ele = $dom->createElement('ele');

$h3 = spl_object_hash($ele);
$h4 = spl_object_hash($dom->documentElement);

var_dump($h1 === $h3); // expected to be false, but actually true
var_dump($h1 === $h4); // expected to be true, but actually false


Expected result:
----------------
bool(true)
bool(true)
bool(false)
bool(true)

Actual result:
--------------
bool(true)
bool(true)
bool(true)
bool(false)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-09-22 22:26 UTC] beberlei@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: beberlei
 [2019-09-22 22:26 UTC] beberlei@php.net
$document->documentElement is not like a public property, but like a magic __set/get property that accepts the element and transaltes it into its internal structure, then "looses" the original element set as argument.

This means, accessing $document->documentElement creates a new DOMElement on the fly.

Hence reference comparisons will not really work with any DOM nodes at all.
 [2019-09-23 06:15 UTC] goetas at gmail dot com
Hi , 
thanks for the update on this!

I can understand that this is the internal behavior (probably connected on how the integration with libxml is done), but it is not intuitive and different from the current OO model exposed by the PHP language.

I can understand that the fix might be not easy, but it should then at least documented that the DOMDocument object might nof follow fully the classica OO behavior.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Sep 27 11:01:23 2020 UTC