Bug #52280 [Opn->Wfx]: LimitIterator cannot take IteratorAggregate inner iterator
| From: | johannes@php.net | Date: | Thu, 08 Jul 2010 10:49:52 +0000 |
| Subject: | Bug #52280 [Opn->Wfx]: LimitIterator cannot take IteratorAggregate inner iterator | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-152215@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=52280&edit=1
ID: 52280
Updated by: johannes@php.net
Reported by: larry at garfieldtech dot com
Summary: LimitIterator cannot take IteratorAggregate inner
iterator
-Status: Open
+Status: Wont fix
Type: Bug
Package: SPL related
Operating System: Any
PHP Version: 5.2.13
New Comment:
You can use new LimitITerator(new IteratorIterator(new Foo))). Doing
this internally adds complexity which can be easily be worked around
from userspace.
Previous Comments:
------------------------------------------------------------------------
[2010-07-07 18:33:15] larry at garfieldtech dot com
Description:
------------
LimitIterator declares in its constructor that it only takes an Iterator
as its inner iterator rather than just a Traversable as its parent
IteratorIterator does. That becomes a problem when, for example, an
object that implements IteratorAggregate is passed into a LimitIterator.
Although IteratorAggregate will produce an Iterator via its
getIterator() method, it is not itself an Iterator, just Traversable, so
LimitIterator will reject it.
I'm running my code on PHP 5.2.6 but according to the documentation this
is an issue in 5.3 still. Technically this behavior is what the
documentation currently reads, but I believe it is a bug that
LimitIterator demands an Iterator, not just a Traversable, and therefore
breaks in this case.
Test script:
---------------
class Foo implements IteratorAggregate {
protected $array = array(1, 2, 3, 4, 5);
public function getIterator() {
return new ArrayIterator($this->array);
}
}
$f = new LimitIterator(new Foo(), 0, 3);
foreach ($f as $a) {
print $a . PHP_EOL;
}
Expected result:
----------------
1
2
3
Actual result:
--------------
Catchable fatal error: Argument 1 passed to LimitIterator::__construct()
must implement interface Iterator, instance of Foo given in
/blah/blah/test.php on line 10
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52280&edit=1