php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64979 Wrong behavior of static variables in closure generators
Submitted: 2013-06-06 05:54 UTC Modified: 2013-06-10 13:55 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: jan at fb dot com Assigned: nikic (profile)
Status: Closed Package: *Programming Data Structures
PHP Version: 5.5.0RC2 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jan at fb dot com
New email:
PHP Version: OS:

 

 [2013-06-06 05:54 UTC] 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


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-10 13:55 UTC] laruence@php.net
-Assigned To: +Assigned To: nikic
 [2013-08-03 15:29 UTC] 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-09-29 18:21 UTC] nikic@php.net
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
 [2013-09-29 18:21 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2013-11-17 09:30 UTC] laruence@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC