#28473 [Bgs]: DOM object custom variables get lost
| From: | rrichards@php.net | Date: | Sun, 23 May 2004 23:56:26 +0000 |
| Subject: | #28473 [Bgs]: DOM object custom variables get lost | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-59923@lists.php.net to get a copy of this message | ||
ID: 28473
Updated by: rrichards@php.net
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:
You are confusing nodes and objects. The DOM Objects provide the DOM
API and are just an interface to the underlying libxml api. So, the
objects refer to the actual node, but in reality nodes are not objects
per say. The specifics of how it all works gets complicated, but when
working on an object you are working on the actual node, but you cant
think of it in reverse, as again nodes are not really objects.
Previous Comments:
------------------------------------------------------------------------
[2004-05-24 00:54:27] bart at mediawave dot nl
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?
------------------------------------------------------------------------
[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