php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30076 PHP fails to copy an array of objects when using the assignment operator
Submitted: 2004-09-13 17:58 UTC Modified: 2004-10-02 16:30 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:0 (0.0%)
From: daphp at mcbf dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4CVS-2004-09-13 (stable) OS: Linux Debian unstable 2.6.8.1
Private report: No CVE-ID: None
 [2004-09-13 17:58 UTC] daphp at mcbf dot net
Description:
------------
When copying an array of objects using the assignment operator '=' in some circumstances not a copy but a reference  is created.
I discovered this when I used a function that had a reference to an object in the array passed modified the object and all over sudden the source of the copy got modified as well.
However, it seems this is not the only way to trigger this behavior, as you can see in the URL provided with the code. It also happens when you use a member function of the object after it got copied.
Please contact me if you need any more information.

Reproduce code:
---------------
<?
class urlfreq {
    var $freq;  // frequency count
}

function upd(&$uf)
{
    $uf->freq++;
}

/********** init *********/
$urls[0][] = new urlfreq();
$urls[0][] = new urlfreq();

$urls[1][] = new urlfreq();
$urls[1][] = new urlfreq();

print("****** Incorrect output, using upd() ********\n");
$std[0][0] = $urls[0][0];
upd($std[0][0]);
$std[0][1] = $urls[0][1];
upd($std[0][1]);

$std[1] = $std[0];
upd($std[1][0]);

print($std[0][0]->freq." < ".$std[1][0]->freq."\n");
unset($std);

print("****** Correct output, using urlfreq::freq++ ********\n");
$std[0][0] = $urls[0][0];
$std[0][0]->freq++;
$std[0][1] = $urls[0][1];
$std[0][1]->freq++;

$std[1] = $std[0];
$std[1][0]->freq++;
print($std[0][0]->freq." < ".$std[1][0]->freq."\n");
// more examples can be found at http://sun.mcbf.net/~squisher/phpbug.phps
?>


Expected result:
----------------
****** Incorrect output, using upd() ********
1 < 2
****** Correct output, using urlfreq::freq++ ********
1 < 2


Actual result:
--------------
****** Incorrect output, using upd() ********
2 < 2
****** Correct output, using urlfreq::freq++ ********
1 < 2


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-02 16:30 UTC] sniper@php.net
See bug #30082 (same issue, simpler example.. = copies multidimensional arrays the wrong way..)

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 16:01:30 2024 UTC