Bug #66248 [NEW]: Object redeclaration

From: Date: Mon, 09 Dec 2013 16:53:33 +0000
Subject: Bug #66248 [NEW]: Object redeclaration
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-183203@lists.php.net to get a copy of this message
From:             info at ferenczigabor dot com
Operating system: 
PHP version:      Irrelevant
Package:          Class/Object related
Bug Type:         Bug
Bug description:Object redeclaration

Description:
------------
I unwittingly instantiated an XML object twice, and I expereienced
peculiar behavior.
(I indicate the irrelevant lines in the description with "..." sign.
The mentioned XML file contains <addresses> elements, and each element
contain a <sent> item:
...
<dataroot>
<asdresses>
...
<address/>
<sent/>
...
</addresses>
<asdresses>
...
<address/>
<sent/>
...
</addresses>
</dataroot>
...).
The PHP version: PHP Version 5.2.4-2ubuntu5.27
I have no chance to upgrade the PHP, because my page is on a provider's
server, which runs this version.


<?php
...
...

$dom = new DOMDocument('1.0', 'iso-8859-2');
...
$dom->load($xml_file);
...
...
...
$dom = new DOMDocument('1.0', 'iso-8859-2');//this is the redeclaration
...
$dom->load($xml_file);
...
...
$address = $dom->getElementsByTagName('addresses');

//Later I filled the "sent" item with the number of sent items:
...
while ($i < $i_counter) ) {
...
$akt_item = $address->item($i);//$i is the actual node in the XML file.
...
$sent_number++;	// the actual naumber of the sent items for the
<address>
$akt_item->getElementsByTagName('sent')->item(0)->nodeValue =
$sent_number;
//for debug reason I printed the new value of the node:
echo $akt_item->getElementsByTagName('sent')->item(0)->nodeValue;
//the printed number was the expected new value, so that $sent_number
...
//Finally I tried to print the changed XML file:
echo $dom->saveXML();
...
?>
The last echo prompt printed out the unchanged XML data! So that all the
<sent> elements' value remained NULL!
(After I had removed the unwanted redeclaration, the code functioned
properly).

This is a bug. Why?
Whenever I declare a variable:
$x = "Some value";
...
then I use this variable again:
$x = "Other value";
From the last decalaration line $x's value strictly will be "Other
value".
But my described code used sometime the first instance of the "$dom" (in
the "saveXML" method), sometime the second instance (in the
"...->nodeValue = ..." and the "echo ...nodeValue"); or v.s.
I guess, the good solution should be to overload the older declaration
with the newest one, or to arise an error.
Gabor Ferenczi


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

$dom = new DOMDocument('1.0', 'iso-8859-2');
...
$dom->load($xml_file);
...
...
...
$dom = new DOMDocument('1.0', 'iso-8859-2');//this is the redeclaration
...
$dom->load($xml_file);
...
...
$address = $dom->getElementsByTagName('addresses');

//Later I filled the "sent" item with the number of sent items:
...
while ($i < $i_counter) ) {
...
$akt_item = $address->item($i);//$i is the actual node in the XML file.
...
$sent_number++;	// the actual naumber of the sent items for the
<address>
$akt_item->getElementsByTagName('sent')->item(0)->nodeValue =
$sent_number;
//for debug reason I printed the new value of the node:
echo $akt_item->getElementsByTagName('sent')->item(0)->nodeValue;
//the printed number was the expected new value, so that $sent_number
...
//Finally I tried to print the changed XML file:
echo $dom->saveXML();
...
?>


-- 
Edit bug report at https://bugs.php.net/bug.php?id=66248&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=66248&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=66248&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=66248&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=66248&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=66248&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=66248&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=66248&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=66248&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=66248&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=66248&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=66248&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=66248&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=66248&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66248&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=66248&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=66248&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=66248&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66248&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=66248&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=66248&r=mysqlcfg



Thread (6 messages)

« previous php.bugs (#183203) next »