php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52640 Odd Garbage collection Behaviour with Dom Node
Submitted: 2010-08-19 00:32 UTC Modified: 2011-02-18 00:24 UTC
From: ken at smallboxcms dot com Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.3.3 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: ken at smallboxcms dot com
New email:
PHP Version: OS:

 

 [2010-08-19 00:32 UTC] ken at smallboxcms dot com
Description:
------------
Variables added to dom nodes are later unset. This behaviour does not appear to happen with other types of PHP objects. 

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

$blah = new stdClass;
function humbug()
{
/* Behaviour changes when uncommented. Think this is a GC bug.
    global $node;
*/

    global $blah;
    $doc = new domDocument('1.0', 'utf-8');
    $blah->doc = $doc;
    $node = $doc->createElement('node');
    $doc->appendChild($node);

    $node->foo = true;

    $obj = new stdClass;
    $blah->obj = $obj;
    $obj->child = new stdClass;
    $obj->child->foo = true;
}

humbug();
echo "Should be set: ".$blah->doc->firstChild->foo."<BR>\n";
echo "Is Set: ".$blah->obj->child->foo."<BR>\n";

?>

Expected result:
----------------
I would expect that $blah->doc->firstChild->foo would be set after the function call. 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-17 22:56 UTC] rrichards@php.net
-Status: Open +Status: Bogus
 [2011-02-17 22:56 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

DOM objects are just wrappers to underlying libxml2 structures and unless directly 
referenced, i.e. $blah->node = $node, the $node object is no longer referenced 
outside of the function. Appending the node appends the libxml2 node structure to 
the underlying xml tree, does nothing in respect to the DOM object wrapper.
 [2011-02-18 00:24 UTC] ken at smallboxcms dot com
Well, to my way of thinking the the XML document is expressed as a PHP object so it should behave as one in every respect. Here is an example where there is a distinct difference in that behaviour, one that would be unexpected by a PHP programmer who is not intimately acquainted with PHP's internals. I am fairly certain that this difference is undocumented. 

Here is another example which more clearly illustrates the problem. 

<?php

$blah = new stdClass;
function humbug()
{
/* Behaviour changes when uncommented. Think this is a GC bug.
    global $node;
*/

    global $blah;
    $doc = new domDocument('1.0', 'utf-8');
    $blah->doc = $doc;
    $node = $doc->createElement('node');
    $doc->appendChild($node);

    $node->foo = true;

    $obj = new stdClass;
    $blah->obj = $obj;
    $obj->child = new stdClass;
    $obj->child->foo = true;

    echo "Why is this set: ".$blah->doc->firstChild->foo."<BR>";
}

humbug();
echo "When this is not set: ".$blah->doc->firstChild->foo."<BR>\n";
echo "This is Set: ".$blah->obj->child->foo."<BR>\n";



?>
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Nov 24 03:01:23 2020 UTC