The CachingIterator class

(PHP 5, PHP 7, PHP 8)

Introduction

This object supports cached iteration over another iterator.

A CachingIterator is always one element ahead of its inner iterator: when it moves to an element, it stores the key and value of that element, and then moves the inner iterator forward. This makes it possible to know whether the current element is the last one, with CachingIterator::hasNext().

With the CachingIterator::FULL_CACHE flag, it also keeps every element that has been iterated over, which can then be accessed with array syntax or retrieved with CachingIterator::getCache().

Class synopsis

class CachingIterator extends IteratorIterator implements ArrayAccess, Countable, Stringable {
/* Constants */
public const int CALL_TOSTRING;
public const int CATCH_GET_CHILD;
public const int TOSTRING_USE_KEY;
public const int TOSTRING_USE_INNER;
public const int FULL_CACHE;
/* Methods */
public function __construct(Iterator $iterator, int $flags = CachingIterator::CALL_TOSTRING)
public function count(): int
public function current(): mixed
public function getCache(): array
public function getFlags(): int
public function hasNext(): bool
public function key(): mixed
public function next(): void
public function offsetExists(string $key): bool
public function offsetGet(string $key): mixed
public function offsetSet(string $key, mixed $value): void
public function offsetUnset(string $key): void
public function rewind(): void
public function setFlags(int $flags): void
public function __toString(): string
public function valid(): bool
/* Inherited methods */
public function IteratorIterator::current(): mixed
public function IteratorIterator::key(): mixed
public function IteratorIterator::next(): void
public function IteratorIterator::rewind(): void
public function IteratorIterator::valid(): bool
}

Predefined Constants

CachingIterator::CALL_TOSTRING

Convert every element to string.

CachingIterator::CATCH_GET_CHILD

Don't throw exception in accessing children.

CachingIterator::TOSTRING_USE_KEY

Use key for conversion to string.

CachingIterator::TOSTRING_USE_CURRENT

Use current for conversion to string.

CachingIterator::TOSTRING_USE_INNER

Use inner for conversion to string.

CachingIterator::FULL_CACHE

Cache all read data.

Changelog

Version Description
8.0.0 CachingIterator implements Stringable now.

Changelog

Version Description
8.4.0 The class constants are now typed.

Table of Contents