Edit report at https://bugs.php.net/bug.php?id=64979&edit=1
ID: 64979 Comment by: ni...@php.net Reported by: jan at fb dot com Summary: Wrong behavior of static variables in closure generators Status: Assigned Type: Bug Package: *Programming Data Structures Operating System: Linux PHP Version: 5.5.0RC2 Assigned To: nikic Block user comment: N Private report: N New Comment: Probably related to the copy of the closure that is made in http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_generators.c#241, which simply uses function_add_ref (http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_compile.c#2983), whereas the closures own code does the copy in a different way: http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_closures.c#459 Previous Comments: ------------------------------------------------------------------------ [2013-06-06 05:54:31] jan at fb dot com Description: ------------ Each closure instance should have its own set of static variables. This works perfectly with non-generator closures (replace yield by return array in the test script), but not with generator closures (run the test script), where static variables seem to behave like a normal local variables. Test script: --------------- <?php function new_closure_gen() { return function() { static $foo = 0; //return array(++$foo); yield ++$foo; }; } $closure1 = new_closure_gen(); $closure2 = new_closure_gen(); $gen1 = $closure1(); $gen2 = $closure1(); $gen3 = $closure2(); foreach (array($gen1, $gen2, $gen3) as $gen) { foreach ($gen as $val) { print "$val\n"; } } Expected result: ---------------- 1 2 1 Actual result: -------------- 1 1 1 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=64979&edit=1