Bug #70682 [Opn->Nab]: extract() removes any references to array
| From: | requinix@php.net | Date: | Sun, 11 Oct 2015 12:13:56 +0000 |
| Subject: | Bug #70682 [Opn->Nab]: extract() removes any references to array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-196524@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70682&edit=1
ID: 70682
Updated by: requinix@php.net
Reported by: marc at phpbb dot com
Summary: extract() removes any references to array
-Status: Open
+Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: Ubuntu Linux
PHP Version: 7.0Git-2015-10-09 (Git)
Block user comment: N
Private report: N
New Comment:
The current behavior seems more correct to me. PHP has had bugs in the past regarding variables
being treated as references when they should not have been - especially when indirectly passing
values to functions through by-ref parameters. PHP 7 includes a number of changes to how variables
and functions work and may have inadvertently fixed this.
Why it's correct?
1. $function does not take an argument by reference, therefore its $test should be a copy. Somehow
managing to modify the returned value (be that by regular assignment or by extract() and touching
the new values) should not affect the $var that was passed.
2. If the parameter is by-ref then the [var] in the return value from extract() will be the same
reference. However the entry in the array is not itself a reference, so when extract() does its work
then the extracted variable will still not be a reference to $var.
Changing $function to be
$function = function(&$test) { return array('var' => &$test); };
produces the expected output.
Previous Comments:
------------------------------------------------------------------------
[2015-10-10 14:59:10] marc at phpbb dot com
Based on a build I compiled myself using the up-to-date git branch, the ticket mentioned in the
initial post seems to be unrelated to the current issue. I was also able to reproduce it on
7.0.0-beta1.
------------------------------------------------------------------------
[2015-10-09 21:00:57] marc at phpbb dot com
Description:
------------
Actual version output (php7 nightly): PHP 7.1.0-dev (cli) (built: Oct 8 2015 19:09:26) ( NTS )
The extract() function removes any references to an array. While the supplied test script works fine
on prior PHP versions like 5.5.x and 5.6.x, it does no longer work on the current PHP7 nightly.
Prior to running extract(), the variable $var and its content is a reference to the initial array
passed to the test() function. After the call to it, this is no longer the case.
According to debug_zval_dump(), the array itself is still a reference. The content of the array
however is not.
I suspect this might have been caused by this as it seems related: https://bugs.php.net/bug.php?id=70250
Test script:
---------------
<?php
function main()
{
$foo = array('meh' => 1);
var_dump($foo);
test($foo);
var_dump($foo);
}
function test(&$var)
{
$function = function($test) { return array('var' => $test); };
extract($function($var));
$var['bar'] = 5;
}
main();
Expected result:
----------------
array(1) {
'meh' =>
int(1)
}
array(2) {
'meh' =>
int(1)
'bar' =>
int(5)
}
Actual result:
--------------
array(1) {
["meh"]=>
int(1)
}
array(1) {
["meh"]=>
int(1)
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70682&edit=1