Bug #55626 [Opn->Bgs]: Closure use variable by ref not work with Iterator
| From: | laruence@php.net | Date: | Wed, 07 Sep 2011 03:15:42 +0000 |
| Subject: | Bug #55626 [Opn->Bgs]: Closure use variable by ref not work with Iterator | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-163347@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55626&edit=1
ID: 55626
Updated by: laruence@php.net
Reported by: xiezhenye at gmail dot com
Summary: Closure use variable by ref not work with Iterator
-Status: Open
+Status: Bogus
Type: Bug
Package: Scripting Engine problem
Operating System: Any
PHP Version: 5.3.8
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2011-09-07 02:53:21] xiezhenye at gmail dot com
missing
$itr = new OneWayIterator($f);
------------------------------------------------------------------------
[2011-09-07 02:52:10] 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: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