CachingIterator::count

(PHP 5 >= 5.2.2, PHP 7, PHP 8)

CachingIterator::count — Return the number of elements in the cache

Description

public function CachingIterator::count(): int

Returns the number of elements in the cache. This is the number of elements iterated over so far, not the number of elements of the inner iterator. Elements are stored in the cache by key, so an element replaces any earlier element with the same key.

The CachingIterator::FULL_CACHE flag must be set.

Parameters

This function has no parameters.

Return Values

Returns the number of elements in the cache.

Errors/Exceptions

Throws a BadMethodCallException if the CachingIterator::FULL_CACHE flag is not set.

Examples

Example #1 CachingIterator::count() example

<?php
$iterator = new CachingIterator(
    new ArrayIterator(['a', 'b', 'c']),
    CachingIterator::FULL_CACHE
);

foreach ($iterator as $value) {
    echo $value, ': ', count($iterator), "\n";
}
?>

The above example will output:

a: 1
b: 2
c: 3

See Also