Edit report at https://bugs.php.net/bug.php?id=55626&edit=1
ID: 55626 Updated by: larue...@php.net Reported by: xiezhenye at gmail dot com Summary: Closure use variable by ref not work with Iterator -Status: Open +Status: Feedback Type: Bug Package: Scripting Engine problem Operating System: Any PHP Version: 5.3.8 Block user comment: N Private report: N New Comment: 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, Previous Comments: ------------------------------------------------------------------------ [2011-09-07 02:45:39] 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 ... ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=55626&edit=1