Edit report at https://bugs.php.net/bug.php?id=72543&edit=1
ID: 72543
Updated by: tony2001@php.net
Reported by: tony2001@php.net
Summary: different references behavior comparing to 5.5
-Status: Open
+Status: Assigned
Type: Bug
Package: Scripting Engine problem
Operating System: *
PHP Version: 7.1.0alpha1
-Assigned To:
+Assigned To: dmitry
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2016-07-04 13:31:50] tony2001@php.net
Description:
------------
Looks like references behavior has changed in 7.x comparing to 5.5.
First of all, once variable becomes reference, it never looses that flag, even though the second
reference is already destroyed (see the first function "create_references").
Second, I'm able to change the original variable by passing its copy to a function and
modifying it there, which is definitely counter-intuitive.
I expected the function to copy the variable to write, since it was not passed by reference, as
that's what seems to be happening in PHP 5.5
Test script:
---------------
/* after this function all array elements become references */
function create_references(&$array) {
$refs[] = &$array;
foreach ($array as $key => $value) {
create_references($array[$key]);
}
}
/* a copy is passed and then modified, which also modifies the original variable */
function change_copy($copy) {
for ($i = 0; $i < 2; $i++) {
$copy['b']['b'] = $copy['b']; //this causes recursion
}
}
$data = [
'a' => [
'b' => [],
],
];
create_references($data);
$copy = $data['a'];
var_dump($data);
change_copy($copy);
var_dump($data); //RECURSION
Expected result:
----------------
array(1) {
["a"]=>
array(1) {
["b"]=>
array(0) {
}
}
}
array(1) {
["a"]=>
array(1) {
["b"]=>
array(0) {
}
}
}
Actual result:
--------------
array(1) {
["a"]=>
array(1) {
["b"]=>
array(0) {
}
}
}
array(1) {
["a"]=>
array(1) {
["b"]=>
array(1) {
["b"]=>
*RECURSION*
}
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72543&edit=1