A reference problem
| From: | Karoly Negyesi | Date: | Sun, 02 Apr 2006 18:36:26 +0000 |
| Subject: | A reference problem | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-22647@lists.php.net to get a copy of this message | ||
Hi!
Without much ado, here is a piece of problematic code:
<?php
ini_set('error_report', E_ALL);
function f1() {
global $a;
$array = array();
$throwaway = f3($array);
var_dump($a);
}
function f2() {
global $a;
$array = array();
f3($array);
var_dump($a);
}
function f3($array) {
global $a;
$array[0] = &$a;
$array[0] = &$array[0]['foo']; // no notice here. interesting.
$array[0] = 'bar';
return $array;
}
$a = array();
f1();
$a = array();
f2();
?>
Please note that the only difference between f1 and f2 that the first records the return value of f3() into a variable. However, this changes the output:
array(1) {
["foo"]=>
&string(3) "bar"
}
array(1) {
["foo"]=>
string(3) "bar"
}
Is this a bug? Or I miss something?
Thanks
Karoly Negyesi
Ps. There is a good reason for using such ugly reference magic, and I am happy to explain it to anyone who is interested, but it's a long story...