CachingIterator::__construct

(PHP 5, PHP 7, PHP 8)

CachingIterator::__construct — Construct a new CachingIterator object for the iterator

Description

public function CachingIterator::__construct(Iterator $iterator, int $flags = CachingIterator::CALL_TOSTRING)

Creates a CachingIterator that iterates over iterator, staying one element ahead of it.

Parameters

iterator

The iterator to cache.

flags

A bitmask of the CachingIterator constants.

At most one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT and CachingIterator::TOSTRING_USE_INNER may be given. It selects what CachingIterator::__toString() returns; if none of them is given, calling it throws an exception.

CachingIterator::FULL_CACHE keeps every element that has been iterated over, so that it can be accessed with array syntax or retrieved with CachingIterator::getCache().

Changelog

Version Description
8.0.0 Now throws a ValueError if flags contains more than one of the string conversion flags; previously, an InvalidArgumentException was thrown.

Notes

Caution

With CachingIterator::CALL_TOSTRING, which is the default, each element is converted to a string as soon as the iterator moves to it, whether or not CachingIterator::__toString() is ever called. An element that is an array raises an E_WARNING, and an object that does not implement __toString() throws an Error. To iterate over such elements, pass flags that do not contain CachingIterator::CALL_TOSTRING, such as 0.

See Also