#39276 [Opn->Bgs]: Unpredictable behaviour of references

From: Date: Fri, 27 Oct 2006 10:13:12 +0000
Subject: #39276 [Opn->Bgs]: Unpredictable behaviour of references
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-104065@lists.php.net to get a copy of this message
ID: 39276 Updated by: tony2001@php.net Reported By: plyrvt at mail dot ru -Status: Open +Status: Bogus Bug Type: Variables related Operating System: any PHP Version: 5.1.6 New Comment: This is expected and predictable. When an array contains references, they still remain references, even if the array is copied. Previous Comments: ------------------------------------------------------------------------ [2006-10-27 10:06:54] plyrvt at mail dot ru Sorry, 1a & 1b were messed up. Correct is: 1a. Element of an array is passed 'by value' and can be modified independently 1b. Whole array can not be passed 'by value'. It is forced to be passed by reference. ------------------------------------------------------------------------ [2006-10-27 09:56:57] plyrvt at mail dot ru Description: ------------ Depending on context, references can have 3 different behaviours: 1. Reference to element of an array. Element becomes reference automatically until that reference exist. 1a. Whole array is passed 'by value' and keys of array copy can be modified independently 1b. Element of an array can not be passed 'by value'. It is forced to be passed by reference. 2. Reference to regular variable. Variable does not become reference and can be passed 'by value' Reproduce code: --------------- <?php function bla1($param){ $param['k1']="new!"; } function bla2($param){ $param="new!";} $a['k1']='foo';; echo var_dump($a); echo "<hr>"; $v=&$a['k1']; echo var_dump($a); echo "<hr>"; bla2($a['k1']); echo var_dump($a); echo "<hr>"; bla1($a); echo var_dump($a); echo "<hr>"; unset($v); echo var_dump($a); echo "<hr>"; $k1='foo'; echo var_dump($k1); echo "<hr>"; $v=&$k1; echo var_dump($k1); echo "<hr>"; bla2($k1); echo var_dump($k1); echo "<hr>"; unset($v); echo var_dump($k1); ?> Expected result: ---------------- any predictable output Actual result: -------------- array(1) { ["k1"]=> string(3) "foo" } array(1) { ["k1"]=> &string(3) "foo" } array(1) { ["k1"]=> &string(3) "foo" } array(1) { ["k1"]=> &string(4) "new!" } array(1) { ["k1"]=> string(4) "new!" } -------------- string(3) "foo" string(3) "foo" string(3) "foo" string(3) "foo" ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=39276&edit=1

« previous php.bugs (#104065) next »