Bug #64979 [Asn->Csd]: Wrong behavior of static variables in closure generators
| From: | nikic@php.net | Date: | Sun, 29 Sep 2013 18:21:10 +0000 |
| Subject: | Bug #64979 [Asn->Csd]: Wrong behavior of static variables in closure generators | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-181828@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64979&edit=1
ID: 64979
Updated by: nikic@php.net
Reported by: jan at fb dot com
Summary: Wrong behavior of static variables in closure
generators
-Status: Assigned
+Status: Closed
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:
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=6b68f44e6b46dffd7fe029eca7afc37f1fa57347
Log: Fix bug #64979: Wrong behavior of static variables in closure generators
Previous Comments:
------------------------------------------------------------------------
[2013-08-03 15:29:00] nikic@php.net
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
------------------------------------------------------------------------
[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