Edit report at http://bugs.php.net/bug.php?id=54039&edit=1
ID: 54039 Updated by: [email protected] Reported by: a at b dot c dot de Summary: use() of static variables in lambda functions can break staticness Status: Feedback Type: Bug Package: Scripting Engine problem Operating System: Windows XP PHP Version: 5.3.5 Block user comment: N Private report: N New Comment: For windows, the address will be http://rmtools.php.net/snaps/5.3-ts-windows-vc9-x86/builds/php-5.3-ts-windows-vc9-x86-r308443.zip (or similar for other compiler/thread safety combination) Previous Comments: ------------------------------------------------------------------------ [2011-02-18 13:54:23] [email protected] Please try using this snapshot: http://snaps.php.net/php5.3-latest.tar.gz For Windows: http://windows.php.net/snapshots/ Bug #53958 is similar and is fixed in SVN. ------------------------------------------------------------------------ [2011-02-18 04:16:43] a at b dot c dot de A little further testing gives this: function test_1() { static $v = ''; $v .= 'b'; echo "Outer function catenates 'b' onto \$v to give $v\n"; $f = function()use($v) { echo "Inner function reckons \$v is $v\n"; }; $v .= 'a'; echo "Outer function catenates 'a' onto \$v to give $v\n"; return $f; } $f = test_1(); $f(); $f = test_1(); $f(); $f = test_1(); $f(); $f = test_1(); $f(); The second concatenation ('a') fails to be retained; if only the anonymous function didn't use($v), the variable would end up containing 'babababa'. ------------------------------------------------------------------------ [2011-02-18 00:49:00] a at b dot c dot de Description: ------------ Let an ordinary function declare a static local variable, and let it also define an anonymous function that uses that static variable. It can happen that the static variable no longer retains its value across calls to the ordinary function; i.e., it loses that static property. It seems to be limited to situations where the anonymous function is defined before the static variable has had its value changed. Test script: --------------- function test_1() { static $v = 0; ++$v; echo "Outer function increments \$v to $v\n"; $f = function()use($v) { echo "Inner function reckons \$v is $v\n"; }; return $f; } echo "\nIncrement static variable, then use it in anonymous function definition:\n"; $f = test_1(); $f(); $f = test_1(); $f(); function test_2() { static $v = 0; $f = function()use($v) { echo "Inner function reckons \$v is $v\n"; }; ++$v; echo "Outer function increments \$v to $v\n"; return $f; } echo "\nUse static variable in anonymous function definition, then increment it:\n"; $f = test_2(); $f(); $f = test_2(); $f(); Expected result: ---------------- Increment static variable, then use it in anonymous function definition: Outer function increments $v to 1 Inner function reckons $v is 1 Outer function increments $v to 2 Inner function reckons $v is 2 Use static variable in anonymous function definition, then increment it: Outer function increments $v to 1 Inner function reckons $v is 0 Outer function increments $v to 2 Inner function reckons $v is 1 Actual result: -------------- Increment static variable, then use it in anonymous function definition: Outer function increments $v to 1 Inner function reckons $v is 1 Outer function increments $v to 2 Inner function reckons $v is 2 Use static variable in anonymous function definition, then increment it: Outer function increments $v to 1 Inner function reckons $v is 0 Outer function increments $v to 1 Inner function reckons $v is 0 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=54039&edit=1
