From:
Operating system: Windows XP
PHP version: 5.3.5
Package: Scripting Engine problem
Bug Type: Bug
Bug description:use() of static variables in lambda functions can break
staticness
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 bug report at http://bugs.php.net/bug.php?id=54039&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=54039&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=54039&r=trysnapshot53
Try a snapshot (trunk):
http://bugs.php.net/fix.php?id=54039&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=54039&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=54039&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=54039&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=54039&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=54039&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=54039&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=54039&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=54039&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=54039&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=54039&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=54039&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=54039&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=54039&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=54039&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=54039&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=54039&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=54039&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=54039&r=mysqlcfg