php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68278 RecursiveIteratorIterator fails when passed an IteratorAggregate
Submitted: 2014-10-21 14:54 UTC Modified: 2014-10-21 19:57 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: chuck at manchuck dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.5.18 OS: windows/*nix
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: chuck at manchuck dot com
New email:
PHP Version: OS:

 

 [2014-10-21 14:54 UTC] chuck at manchuck dot com
Description:
------------
When RecursiveIteratorIterator is given an IteratorAggregate, a fatal exception is thrown.



Test script:
---------------
class IteratorBug implements IteratorAggregate
{
    public function getIterator()
    {
        return new ArrayIterator([]);
    }
}

$iterator = new RecursiveIteratorIterator(new IteratorBug());

Expected result:
----------------
a RecursiveIteratorIterator is created

Actual result:
--------------
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'An instance of RecursiveIterator or IteratorAggregate creating it is required' in /var/www/sando/bin/iteratorBug.php on line 11

InvalidArgumentException: An instance of RecursiveIterator or IteratorAggregate creating it is required in /var/www/sando/bin/iteratorBug.php on line 11


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-21 15:00 UTC] chuck at manchuck dot com
This will also cause the same error:

$iterator = new RecursiveIteratorIterator(new \ArrayObject([]));
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'An instance of RecursiveIterator or IteratorAggregate creating it is required' in /var/www/sando/bin/iteratorBug.php on line 2

InvalidArgumentException: An instance of RecursiveIterator or IteratorAggregate creating it is required in /var/www/sando/bin/iteratorBug.php on line 2


ArrayObject does implement IteratorAggregate:
$  php -r 'var_dump(class_implements("\ArrayObject"));'
array(5) {
  'IteratorAggregate' =>
  string(17) "IteratorAggregate"
  'Traversable' =>
  string(11) "Traversable"
  'ArrayAccess' =>
  string(11) "ArrayAccess"
  'Serializable' =>
  string(12) "Serializable"
  'Countable' =>
  string(9) "Countable"
}
 [2014-10-21 19:21 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-10-21 19:21 UTC] requinix@php.net
RecursiveIteratorIterator is an iterator over other recursive iterators. As such it needs to be given either (a) a RecursiveIterator or (b) an IteratorAggregate that produces a RecursiveIterator.

ArrayIterator is not recursive, thus the error. Use RecursiveArrayIterator instead.
http://3v4l.org/Ia7qL
 [2014-10-21 19:48 UTC] chuck at manchuck dot com
ArrayObject is an IteratorAggregate and this bug still presents itself:

$iterator = new RecursiveIteratorIterator(new \ArrayObject([]));
 [2014-10-21 19:57 UTC] requinix@php.net
Yes, ArrayObject is an IteratorAggregate, but its getIterator() method does not return a recursive iterator. That's the problem. It has to return a recursive iterator to be valid.
 [2014-10-21 20:05 UTC] chuck at manchuck dot com
Ok thanks for clearing that up.  I see where what you are getting at. 

This worked just fine: 

class IteratorBug implements IteratorAggregate
{
    public function getIterator()
    {
        return new RecursiveArrayIterator(['foo' => [1,2,3], 'bar' => [1,2,3]]);
    }
}

$iterator = new RecursiveIteratorIterator(new IteratorBug());
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 09:01:33 2025 UTC