Bug #55626 [NEW]: Closure use variable by ref not work with Iterator
| From: | xiezhenye at gmail dot com | Date: | Wed, 07 Sep 2011 02:45:39 +0000 |
| Subject: | Bug #55626 [NEW]: Closure use variable by ref not work with Iterator | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-163344@lists.php.net to get a copy of this message | ||
From:
Operating system: Any
PHP version: 5.3.8
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Closure use variable by ref not work with Iterator
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 bug report at https://bugs.php.net/bug.php?id=55626&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=55626&r=trysnapshot54
Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=55626&r=trysnapshot53
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=55626&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=55626&r=fixed
Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=55626&r=needdocs
Fixed in release: https://bugs.php.net/fix.php?id=55626&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=55626&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=55626&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=55626&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=55626&r=support
Expected behavior: https://bugs.php.net/fix.php?id=55626&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=55626&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=55626&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=55626&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=55626&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=55626&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=55626&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=55626&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=55626&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=55626&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=55626&r=mysqlcfg