php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55626 Closure use variable by ref not work with Iterator
Submitted: 2011-09-07 02:45 UTC Modified: 2011-09-07 03:15 UTC
From: xiezhenye at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.8 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: xiezhenye at gmail dot com
New email:
PHP Version: OS:

 

 [2011-09-07 02:45 UTC] xiezhenye at gmail dot com
Description:
------------
Closure use variable by ref not work with Iterator.

when use foreach on an Iterator used a Closure, the variable used by ref seems used by val.


Test script:
---------------
class OneWayIterator implements Iterator {
    protected $end = false;
    protected $getNext = null;
    protected $value = null;
    
    function __construct($next) {
        $this->getNext = $next;
    }
    
    function current() {
        return $this->value;
    }
    
    function next() {
        try {
            $this->value = call_user_func($this->getNext);
        } catch (IteratorEndException $e) {
            $this->end = true;
        }
    }
    
    function key() {
    }
    
    function valid() {
        return !$this->end;
    }
    
    function rewind() {
        try {
            $this->value = call_user_func($this->getNext);
        } catch (IteratorEndException $e) {
            $this->end = true;
        }
    }
}
class IteratorEndException extends Exception {
    
}
$i = 0;
$f = function() use (&$i) {
    if ($i >= 10) {
        throw new IteratorEndException();
    }
    return $i++;
};

foreach ($itr as $i) {
    echo "$i\n";
}


Expected result:
----------------
0
1
2
3
4
5
6
7
8
9


Actual result:
--------------
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-09-07 02:52 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2011-09-07 02:52 UTC] laruence@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

your test script seems wrong, since $irs is undefined variables,
 [2011-09-07 02:53 UTC] xiezhenye at gmail dot com
-Status: Feedback +Status: Open
 [2011-09-07 02:53 UTC] xiezhenye at gmail dot com
missing 
$itr = new OneWayIterator($f);
 [2011-09-07 03:15 UTC] laruence@php.net
-Status: Open +Status: Bogus
 [2011-09-07 03:15 UTC] laruence@php.net
there is no "block scope" in php, you should use another variable name to make the test works "as expected", like:
foreach ($itr as $mmm) {
    echo "$mmm\n";
}

thanks
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 09:01:32 2025 UTC