php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52280 LimitIterator cannot take IteratorAggregate inner iterator
Submitted: 2010-07-07 18:33 UTC Modified: 2010-07-08 12:49 UTC
From: larry at garfieldtech dot com Assigned:
Status: Wont fix Package: SPL related
PHP Version: 5.2.13 OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: larry at garfieldtech dot com
New email:
PHP Version: OS:

 

 [2010-07-07 18:33 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-08 12:49 UTC] johannes@php.net
-Status: Open +Status: Wont fix
 [2010-07-08 12:49 UTC] johannes@php.net
You can use new LimitITerator(new IteratorIterator(new Foo))). Doing this internally adds complexity which can be easily be worked around from userspace.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 18:02:40 2024 UTC