The CachingIterator class

- « ArrayIterator::valid

- CachingIterator::__construct »

- PHP Manual
- Iterators
- The CachingIterator class

# The CachingIterator class

(PHP 5, PHP 7, PHP 8)

## Introduction

This object supports cached iteration over another iterator.

## 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_CURRENT;
public const int TOSTRING_USE_INNER;
public const int FULL_CACHE;
/* Methods */
public __construct(Iterator `$iterator`, int `$flags` = CachingIterator::CALL_TOSTRING)
public count(): int
public current(): mixed
public getCache(): array
public getFlags(): int
public hasNext(): bool
public key(): scalar
public next(): void
public offsetExists(string `$key`): bool
public offsetGet(string `$key`): mixed
public offsetSet(string `$key`, mixed `$value`): void
public offsetUnset(string `$key`): void
public rewind(): void
public setFlags(int `$flags`): void
public __toString(): string
public valid(): bool
/* Inherited methods */
public IteratorIterator::current(): mixed
public IteratorIterator::getInnerIterator(): ?Iterator
public IteratorIterator::key(): mixed
public IteratorIterator::next(): void
public IteratorIterator::rewind(): void
public 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.

## Table of Contents

- CachingIterator::__construct — Construct a new CachingIterator object for the iterator
- CachingIterator::count — The number of elements in the iterator
- CachingIterator::current — Return the current element
- CachingIterator::getCache — Retrieve the contents of the cache
- CachingIterator::getFlags — Get flags used
- CachingIterator::hasNext — Check whether the inner iterator has a valid next element
- CachingIterator::key — Return the key for the current element
- CachingIterator::next — Move the iterator forward
- CachingIterator::offsetExists — The offsetExists purpose
- CachingIterator::offsetGet — The offsetGet purpose
- CachingIterator::offsetSet — The offsetSet purpose
- CachingIterator::offsetUnset — The offsetUnset purpose
- CachingIterator::rewind — Rewind the iterator
- CachingIterator::setFlags — The setFlags purpose
- CachingIterator::__toString — Return the string representation of the current element
- CachingIterator::valid — Check whether the current element is valid