|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-10-09 21:00 UTC] 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) } PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 21 23:00:01 2025 UTC |
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.