Bug #71872 [Opn]: PHP registerNodeClass and reusing variable names
| From: | cmb@php.net | Date: | Thu, 05 Nov 2020 15:24:27 +0000 |
| Subject: | Bug #71872 [Opn]: PHP registerNodeClass and reusing variable names | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-230134@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=71872&edit=1
ID: 71872
Updated by: cmb@php.net
Reported by: jacksonpauls at gmail dot com
Summary: PHP registerNodeClass and reusing variable names
Status: Open
Type: Bug
Package: DOM XML related
Operating System: Linux - Fedora 23
PHP Version: 7.0.6
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Very good explanation by Ruslam! I'm afraid that cannot be fixed,
though, since it would require to keep references from the
internal libxml2 structures (which may be possible), and to
dereference when these structures are freed (what is not possible
to my knowledge). As such I tend to change this to documentation
issue.
Previous Comments:
------------------------------------------------------------------------
[2016-05-11 14:57:28] jacksonpauls at gmail dot com
There's now a detailed explanation of why this happens here: http://stackoverflow.com/a/37087295/1777662
------------------------------------------------------------------------
[2016-03-21 16:29:16] jacksonpauls at gmail dot com
Description:
------------
When registering a new base node type with registerNodeClass: if I re-use variable names for created
elements, then custom properties revert to their default value.
Test script:
---------------
<?php
class myDOMElement extends DOMElement
{
public $myProp = 'Some default';
}
$doc = new DOMDocument();
$doc->registerNodeClass('DOMElement', 'myDOMElement');
$node = $doc->createElement('a');
$node->myProp = 'A';
$doc->appendChild($node);
# This seems to alter node A in $doc, not what I expected:
$node = $doc->createElement('b');
$node->myProp = 'B';
$doc->appendChild($node);
# Note: $nodeC instead of $node, this works fine.
$nodeC = $doc->createElement('c');
$nodeC->myProp = 'C';
$doc->appendChild($nodeC);
foreach ($doc->childNodes as $n) {
echo 'Tag ', $n->tagName, ' myProp:', PHP_EOL;
var_dump($n->myProp);
}
Expected result:
----------------
Tag a myProp:
string(1) "A"
Tag b myProp:
string(1) "B"
Tag c myProp:
string(1) "C"
Actual result:
--------------
Tag a myProp:
string(12) "Some default"
Tag b myProp:
string(1) "B"
Tag c myProp:
string(1) "C"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71872&edit=1