Bug #66248 [Opn->Fbk]: Object redeclaration

From: Date: Mon, 09 Dec 2013 18:42:08 +0000
Subject: Bug #66248 [Opn->Fbk]: Object redeclaration
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-183204@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66248&edit=1 ID: 66248 Updated by: requinix@php.net Reported by: info at ferenczigabor dot com Summary: Object redeclaration -Status: Open +Status: Feedback Type: Bug Package: Class/Object related PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. -- I get the correct (expected) behavior using <?php $xml = <<<XML <root><node1><node2>empty</node2></node1></root> XML; $dom = new DOMDocument("1.0", "iso-8859-2"); $dom->loadXML($xml); $dom = new DOMDocument("1.0", "iso-8859-2"); $dom->loadXML($xml); $node1 = $dom->getElementsByTagName("node1")->item(0); $node1->getElementsByTagName("node2")->item(0)->nodeValue = "full"; echo $dom->saveXML(); // <root><node1><node2>full</node2></node1></root> ?> Previous Comments: ------------------------------------------------------------------------ [2013-12-09 16:53:32] info at ferenczigabor dot com 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 this bug report at https://bugs.php.net/bug.php?id=66248&edit=1

« previous php.bugs (#183204) next »