The CachingIterator class

(PHP 5, PHP 7)


This object supports cached iteration over another iterator.

클래스 개요

CachingIterator extends IteratorIterator implements OuterIterator , ArrayAccess , Countable {
/* 상수 */
const integer CALL_TOSTRING = 1 ;
const integer CATCH_GET_CHILD = 16 ;
const integer TOSTRING_USE_KEY = 2 ;
const integer TOSTRING_USE_CURRENT = 4 ;
const integer TOSTRING_USE_INNER = 8 ;
const integer FULL_CACHE = 256 ;
/* 메소드 */
public __construct ( Iterator $iterator [, int $flags = self::CALL_TOSTRING ] )
public int count ( void )
public void current ( void )
public array getCache ( void )
public int getFlags ( void )
public Iterator getInnerIterator ( void )
public void hasNext ( void )
public scalar key ( void )
public void next ( void )
public void offsetExists ( string $index )
public void offsetGet ( string $index )
public void offsetSet ( string $index , string $newval )
public void offsetUnset ( string $index )
public void rewind ( void )
public void setFlags ( int $flags )
public void __toString ( void )
public void valid ( void )

예약 상수


Convert every element to string.


Don't throw exception in accessing children.


Use key for conversion to string.


Use current for conversion to string.


Use inner for conversion to string.


Cache all read data.

Table of Contents

add a note add a note

User Contributed Notes 4 notes

ahmad dot mayahi at gmail dot com
5 years ago
//This snippet will print out all the cached elements (foreach) .

$cache  = new CachingIterator(new ArrayIterator(range(1,100)), CachingIterator::FULL_CACHE);

foreach (
$cache as $c) {


ahmad dot mayahi at gmail dot com
2 years ago
The only difference between CachingIterator and other Iterators such as ArrayIterator is the hasNext() method.

Since the data will be loaded into the memory, the CachingIterator is able to check whether the given iterator has a next element.

Let's demonstrate this by an example:

= new CachingIterator(new ArrayIterator(['C', 'C++', 'C#', 'PHP', 'Python', 'Go', 'Ruby']));

foreach (
$iterator as $item) {
    if (
$iterator->hasNext()) {
$item.', ';
    } else {
'and '.$item;

// C, C++, C#, PHP, Python, Go, and Ruby

In this example I check whether the iterator has a next value, if so, I append a comma otherwise "and" will be appended to the last element.
xedin dot unknown at gmail dot com
2 years ago
Apparently, the `FULL_CACHE` flag automatically cancels the default flag `CALL_TOSTRING`. This is evident when one of the values cannot be converted to string: with the default `CALL_TOSTRING` flag, it would throw an error; without that flag, or with the `FULL_CACHE` flag, it does not.
jerome at chaman dot ca
2 years ago
"cached iteration over another iterator" means this iterator is always one step behind the inner iterator. In other words, the "first" iteration will yield null:


= new CachingIterator( new ArrayIterator( [ 'a', 'b', 'c']  ) );

$cit->current() ); // null
echo $cit->getInnerIterator()->current() ); // "a"

// we start with a "next" since the "first" item is null
$cit->current(), '<br>';

iterating this way gives us an access, ahead, to the future item (aka current item of the inner iterator)
To Top