Bug #72543 [NEW]: different references behavior comparing to 5.5
From: tony2001
Operating system: *
PHP version: 7.1.0alpha1
Package: Scripting Engine problem
Bug Type: Bug
Bug description:different references behavior comparing to 5.5
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 bug report at https://bugs.php.net/bug.php?id=72543&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72543&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72543&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72543&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=72543&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=72543&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=72543&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=72543&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=72543&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=72543&r=support
Expected behavior: https://bugs.php.net/fix.php?id=72543&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=72543&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=72543&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=72543&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72543&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=72543&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=72543&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=72543&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72543&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=72543&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=72543&r=mysqlcfg
Thread (12 messages)
- tony2001@php.net
- tony2001@php.netBug #72543 [Opn->Asn]: different references behavior comparing to 5.5
- laruence@php.netBug #72543 [Asn]: different references behavior comparing to 5.5
- dmitry@php.netBug #72543 [Asn]: different references behavior comparing to 5.5
- laruence@php.netBug #72543 [Asn]: different references behavior comparing to 5.5
- dmitry@php.netBug #72543 [Asn]: different references behavior comparing to 5.5
- dmitry@php.netBug #72543 [PATCH]: different references behavior comparing to 5.5
- laruence@php.netBug #72543 [Asn]: different references behavior comparing to 5.5
- dmitry@php.netBug #72543 [PATCH]: different references behavior comparing to 5.5
- dmitry@php.netBug #72543 [PATCH]: different references behavior comparing to 5.5
- dmitry@php.netBug #72543 [Asn->Csd]: different references behavior comparing to 5.5
- dmitry@php.netBug #72543 [Csd]: different references behavior comparing to 5.5