note 79220 added to language.references
| From: | marco at greenlightsolutions dot nl | Date: | Thu, 15 Nov 2007 22:38:17 +0000 |
| Subject: | note 79220 added to language.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132333@lists.php.net to get a copy of this message | ||
I ran into a bit of a problem recently, with an array copy resulting in a reference copy of one of
the elements instead of a clone. Sample code:
<?php
$a=array(1 => "A");
$b=&$a[1];
$c=$a; // should be a deep cloning
$c[1]="C";
var_dump($a[1]); // yields 'C' instead of 'A'
?>
After some searching, I found that it was a known bug which would be too costly to fix (see http://bugs.php.net/bug.php?id=20993). There was
supposed to be some documentation on this behaviour on this page:
"Due to peculiarities of the internal workings of PHP, if a reference is made to a single
element of an array and then the array is copied, whether by assignment or when passed by value in a
function call, the reference is copied as part of the array. This means that changes to any such
elements in either array will be duplicated in the other array (and in the other references), even
if the arrays have different scopes (e.g. one is an argument inside a function and the other is
global)! Elements that did not have references at the time of the copy, as well as references
assigned to those other elements after the copy of the array, will behave normally (i.e. independent
of the other array)."
However, this paragraph appears to have been removed from this page at some point, presumably
because it was a bit obscure. The comments section seem to be a proper place for this, though.
----
Server IP: 69.147.83.197
Probable Submitter: 82.95.191.164
----
Manual Page -- http://www.php.net/manual/en/language.references.php
Edit -- https://master.php.net/note/edit/79220
Del: integrated -- https://master.php.net/note/delete/79220/integrated
Del: useless -- https://master.php.net/note/delete/79220/useless
Del: bad code -- https://master.php.net/note/delete/79220/bad+code
Del: spam -- https://master.php.net/note/delete/79220/spam
Del: non-english -- https://master.php.net/note/delete/79220/non-english
Del: in docs -- https://master.php.net/note/delete/79220/in+docs
Del: other reasons-- https://master.php.net/note/delete/79220
Reject -- https://master.php.net/note/reject/79220
Search -- https://master.php.net/manage/user-notes.php