#28473 [Bgs]: DOM object custom variables get lost
ID: 28473
User updated by: bart at mediawave dot nl
Reported By: bart at mediawave dot nl
Status: Bogus
Bug Type: DOM XML related
Operating System: Windows XP
PHP Version: 5.0.0RC2
New Comment:
So, this means that, when I use the dom functions to find a node. This
node actually is sort of a copy in stead of the actual node inside the
domDocument?
I'm expecting that all the nodes, elements and attributes are the
actual nodes, elements and attributes of the domDocument. Not temporary
PHP instances of the actual domDocument objects?
Previous Comments:
------------------------------------------------------------------------
[2004-05-24 00:27:40] 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
It all has to do with object lifespans. ->query creates a static set of
objects so they live as long as either the returned nodeset exists or
the contained objects are referenced (so the width attribute works as
you expected).
getElementsByXXX however is a live function (per specs), so does not
create any objects unless a node is explicity called for, which means
once your object goes out of scope ($td in the foreach loops), the
custom properties are gone.
------------------------------------------------------------------------
[2004-05-21 14:41:59] bart at mediawave dot nl
Description:
------------
Setting custom variables in DOM objects like:
$domNode->customVar = 'myValue';
Are inconsistently lost when working with a dom.
Reproduce code:
---------------
<?php
class myclass {
function run() {
$xp = new domxpath($this->dom);
$widths = $xp->query('//td/@width');
foreach ($widths as $width) {
echo $this->name.' 1: width->myVar
value:'.$width->myVar.'<br>';
$width->myVar = '1';
}
$tds = $this->dom->getElementsByTagName('td');
foreach ($tds as $td) {
echo $this->name.' 2: td->myVar value:'.$td->myVar.'<br>';
$td->myVar = '1';
}
echo '<br>';
$tds = $this->dom->getElementsByTagName('td');
foreach ($tds as $td) {
if ($width = $td->getAttributeNode('width')) {
echo $this->name.' 3: width->myVar
value:'.$width->myVar.'<br>';
$width->myVar = '2';
}
echo $this->name.' 4: td->myVar value:'.$td->myVar.'<br>';
$td->myVar = '2';
}
echo '<br>';
}
}
$xml = '<root>
<tr><td width="40">hello</td><td
width="40">world</td></tr>
<tr><td>Just one TD</td></tr>
<tr><td>TD with <b>mixed</b>
<i>content</i></td></tr>
</root>';
$dom = new DomDocument;
$dom->loadXML($xml);
$firstInstance = new myclass;
$firstInstance->name = 'firstInstance';
$firstInstance->dom = $dom;
$firstInstance->run();
$secondInstance = new myclass;
$secondInstance->name = 'secondInstance';
$secondInstance->dom = $firstInstance->dom;
$secondInstance->run();
?>
Expected result:
----------------
firstInstance 1: width->myVar value:
firstInstance 1: width->myVar value:
firstInstance 2: td->myVar value:
firstInstance 2: td->myVar value:
firstInstance 2: td->myVar value:
firstInstance 2: td->myVar value:
firstInstance 3: width->myVar value:1
firstInstance 4: td->myVar value:1
firstInstance 3: width->myVar value:1
firstInstance 4: td->myVar value:1
firstInstance 4: td->myVar value:1
firstInstance 4: td->myVar value:1
secondInstance 1: width->myVar value:2
secondInstance 1: width->myVar value:2
secondInstance 2: td->myVar value:2
secondInstance 2: td->myVar value:2
secondInstance 2: td->myVar value:2
secondInstance 2: td->myVar value:2
secondInstance 3: width->myVar value:1
secondInstance 4: td->myVar value:1
secondInstance 3: width->myVar value:1
secondInstance 4: td->myVar value:1
secondInstance 4: td->myVar value:1
secondInstance 4: td->myVar value:1
Actual result:
--------------
firstInstance 1: width->myVar value:
firstInstance 1: width->myVar value:
firstInstance 2: td->myVar value:
firstInstance 2: td->myVar value:
firstInstance 2: td->myVar value:
firstInstance 2: td->myVar value:
firstInstance 3: width->myVar value:1
firstInstance 4: td->myVar value:
firstInstance 3: width->myVar value:1
firstInstance 4: td->myVar value:
firstInstance 4: td->myVar value:
firstInstance 4: td->myVar value:
secondInstance 1: width->myVar value:
secondInstance 1: width->myVar value:
secondInstance 2: td->myVar value:
secondInstance 2: td->myVar value:
secondInstance 2: td->myVar value:
secondInstance 2: td->myVar value:
secondInstance 3: width->myVar value:1
secondInstance 4: td->myVar value:
secondInstance 3: width->myVar value:1
secondInstance 4: td->myVar value:
secondInstance 4: td->myVar value:
secondInstance 4: td->myVar value:
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=28473&edit=1
Thread (6 messages)