#24630 [Bgs->Opn]: extract with EXTR_REFS makes weird references

From: Date: Sun, 13 Jul 2003 02:40:26 +0000
Subject: #24630 [Bgs->Opn]: extract with EXTR_REFS makes weird references
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-43846@lists.php.net to get a copy of this message
ID: 24630 User updated by: tater at potatoe dot com Reported By: tater at potatoe dot com -Status: Bogus +Status: Open Bug Type: Zend Engine 2 problem Operating System: OS X 10.2 PHP Version: 5.0.0b2-dev New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2003-07-12 21:04:27] sniper@php.net Try search the bug database before submitting new reports.. there are couple of related reports still open. (mostly documentation issues..) ------------------------------------------------------------------------ [2003-07-12 20:47:59] 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. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=24630&edit=1

« previous php.bugs (#43846) next »