|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-10-02 16:30 UTC] sniper@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 08:00:01 2025 UTC |
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