From:             daphp at mcbf dot net
Operating system: Linux Debian unstable 2.6.8.1
PHP version:      4CVS-2004-09-13 (stable)
PHP Bug Type:     Arrays related
Bug description:  PHP fails to copy an array of objects when using assignment

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 bug report at http://bugs.php.net/?id=30076&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=30076&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=30076&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=30076&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=30076&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=30076&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=30076&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=30076&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=30076&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=30076&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=30076&r=notwrong
Not enough info:             http://bugs.php.net/fix.php?id=30076&r=notenoughinfo
Submitted twice:             http://bugs.php.net/fix.php?id=30076&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=30076&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=30076&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=30076&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=30076&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=30076&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=30076&r=float
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=30076&r=mysqlcfg

Reply via email to