php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66248 Object redeclaration
Submitted: 2013-12-09 16:53 UTC Modified: 2013-12-10 18:00 UTC
From: info at ferenczigabor dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: info at ferenczigabor dot com
New email:
PHP Version: OS:

 

 [2013-12-09 16:53 UTC] 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();
...
?>


Patches

file_xml (last revision 2013-12-10 11:17 UTC by info at ferenczigabor dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-09 18:42 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2013-12-09 18:42 UTC] requinix@php.net
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>

?>
 [2013-12-10 08:22 UTC] info at ferenczigabor dot com
-Status: Feedback +Status: Open
 [2013-12-10 08:22 UTC] info at ferenczigabor dot com
<?php
//the entire code's language is Hungarian, please excuse me for the funny mixture of the English and Hungarian
define("max_mail",10);
define("CrLf","\r\n");
define("Cr","\n");

//link for the xml file: http://paskal-bt.hu/bakny
$xml_file = "http://paskal-bt.hu/bakny/PHP-cimlista-proba.xml";
$xml_sablon = "http://paskal-bt.hu/bakny/PHP-xml_sablon.php";

$dom = new DOMDocument('1.0', 'iso-8859-2');
if (!file_exists($xml_file)) {
$dom->loadXML($xml_sablon);
$dom->save($xml_file);
} //if f_exists 
else {
$dom->load($xml_file);
}	//else
$cimlista = $dom->getElementsByTagName('Cimlista');
$napi_mail_szam = 0;

$dom = new DOMDocument('1.0', 'ISO-8859-2');	//this is the second instanciateting of the $dom object
if (!$dom->load($xml_file)) {
  echo "Error while parsing the document: $xml_file\n";
  exit;
}

$dom->formatOutput = true;

$items = $dom->getElementsByTagName('E-mail');
for ($i=0; $i < $items->length ; $i++) {
$mail_items_tomb[$i] = $items->item($i)->nodeValue;
}//for i

$items = $dom->getElementsByTagName('Elkuldve');
for ($i=0; $i < $items->length ; $i++) {
$elkuldve_items_tomb[$i] = $items->item($i)->nodeValue;
}//for i
$i = 0;	$tomb_meret = count($mail_items_tomb);
$elkuldve_valasz = "1";

while ($i < $tomb_meret)  {
$akt_elem = $cimlista->item($i);

$siker = true;	//in the entire PHP code $siker is the result of a mail() function. For the 20 lines maximum code-size I deleted all the lines, which are unrelevants.
if ($siker) {
$akt_elem->getElementsByTagName('Elkuldve')->item(0)->nodeValue = $elkuldve_valasz;
echo $akt_elem->getElementsByTagName('Elkuldve')->item(0)->nodeValue;	//this line is not in the entire code, I inserted only for demonstrating the bug.
}	//if siker
++$i;
}	//while vége
$dom->saveXML();//this line is not in the entire code, I inserted only for demonstrating the bug.
$dom->normalize;
//$dom->save($xml_file);	//the entire code save the new xml database file here. I commented out this line only for the PHP demonstration.
echo "Az eredmény mentve";
?>
 [2013-12-10 09:29 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2013-12-10 09:29 UTC] requinix@php.net
$xml_file = "http://paskal-bt.hu/bakny/PHP-cimlista-proba.xml";
$dom->save($xml_file);

You cannot save to a file over the internet. Change $xml_file to be a local file path, and a file path that your script has write access to. Any change in behavior?
 [2013-12-10 11:18 UTC] info at ferenczigabor dot com
-Status: Feedback +Status: Open
 [2013-12-10 11:18 UTC] info at ferenczigabor dot com
How can I send the xml files? I can't see any form-field, or button for sending or upload files? Is it good the "Add a Patch" button? T'm gona to send on that. Please delete the first line, than rename that as xml.
 [2013-12-10 18:00 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2013-12-10 18:00 UTC] requinix@php.net
That's not what I'm talking about.

Please find a mailing list or forum to get further help with your script. In the mean time I'm still quite sure this isn't a bug with PHP but with your code instead.
http://www.php.net/support.php
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Mar 23 18:01:29 2025 UTC