php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #71872 PHP registerNodeClass and reusing variable names
Submitted: 2016-03-21 16:29 UTC Modified: 2020-11-05 15:40 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: jacksonpauls at gmail dot com Assigned: cmb (profile)
Status: Closed Package: DOM XML related
PHP Version: 7.0.6 OS: Linux - Fedora 23
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
38 - 34 = ?
Subscribe to this entry?

 
 [2016-03-21 16:29 UTC] 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"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-11 14:57 UTC] jacksonpauls at gmail dot com
-PHP Version: 7.0.4 +PHP Version: 7.0.6
 [2016-05-11 14:57 UTC] jacksonpauls at gmail dot com
There's now a detailed explanation of why this happens here: http://stackoverflow.com/a/37087295/1777662
 [2020-11-05 15:24 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2020-11-05 15:24 UTC] cmb@php.net
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.
 [2020-11-05 15:40 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2020-11-06 11:04 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=c410fb889580a55ee09aa728a0ad4770df40e127
Log: Fix #71872: PHP registerNodeClass and reusing variable names
 [2020-11-06 11:04 UTC] phpdocbot@php.net
-Status: Assigned +Status: Closed
 [2020-11-07 05:20 UTC] phpdocbot@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=c14e74f443ec45daed07c2d1270d1bb8959b5cde
Log: Fix #71872: PHP registerNodeClass and reusing variable names
 [2020-12-30 11:58 UTC] nikic@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=6049faa7a023dfd8eaf8593b571ca4f29dacf8ec
Log: Fix #71872: PHP registerNodeClass and reusing variable names
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 22:01:28 2024 UTC