|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-07-12 20:47 UTC] tater at potatoe dot com
Description:
------------
If you create an array by assigning its elements the value of a scalar variable (or, in PHP5, by using array_combine() with an array of keys and an array built with array_fill() or array_pad()), then extract(array, EXTR_REFS) will make that scalar into a reference, as well as making all the array elements references to the same... location? I don't know, they all end up pointing to the same thing, so that changing any one of them changes all of them.
This tests the same on the current PHP4 release and the latest CVS PHP5 code.
Reproduce code:
---------------
<?php
function trap()
{
$tick = 0;
$x = array('x1'=>$tick, 'x2'=>$tick, 'x3'=>0, 'x4'=>0);
$y = array('y1'=>$tick, 'y2'=>$tick, 'y3'=>0, 'y4'=>0);
var_dump(get_defined_vars());
foreach ($x as $k => $v)
$$k =& $x[$k];
extract($y, EXTR_REFS);
var_dump(get_defined_vars());
$x1 = 1;
$y1 = 2;
$y3 = 4;
var_dump(get_defined_vars());
$tick = 5;
var_dump(get_defined_vars());
}
trap();
?>
Expected result:
----------------
Changing $y1 should only change it and y['y1'].
Changing $tick should only change $tick.
Actual result:
--------------
Changing $y1 or $tick changes the other as well.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Sat Feb 07 16:00:01 2026 UTC |
OK, tell me this is bogus: ------------------------------- <?php function trap1() { $a = 10; $b = array('x'=>$a); $x =& $b['x']; $x = 5; var_dump(get_defined_vars()); } trap1(); function trap2() { $a = 10; $b = array('x'=>$a); extract($b, EXTR_REFS); $x = 5; var_dump(get_defined_vars()); } trap2(); ?> In first function, $a is unchanged. In second, it is altered. They should behave the same way. It is only extract() making the difference.IMO, it's exactly how it's supposed to work. To emulate the EXTR_REFS: <?php function trap1() { $a = 10; $b = array('x'=>&$a); $x =& $b['x']; $x = 5; var_dump(get_defined_vars()); } trap1();But '$b = array('x'=>&$a);' is quite different from '$b = array('x'=>$a);', and extract() is retroactively altering the definition of $b. $b['x'] should just be a copy of $a. I think it's just a symbol table problem. If I do: $a = 10; $b['x'] = $a; $a = 10; extract($b, EXTR_REFS); $x = 5; now $a is not changed. If $b['x'] really was &$a, that would not be the case.