ID:               30076
 User updated by:  daphp at mcbf dot net
-Summary:          PHP fails to copy an array of objects when using
                   assignment
 Reported By:      daphp at mcbf dot net
 Status:           Open
 Bug Type:         Arrays related
 Operating System: Linux Debian unstable 2.6.8.1
 PHP Version:      4CVS-2004-09-13 (stable)
 New Comment:

Fixed bad english in the summary.


Previous Comments:
------------------------------------------------------------------------

[2004-09-13 17:58:44] 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



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=30076&edit=1

Reply via email to