Skip to content

ChainIterableIterator

github-actions edited this page Mar 31, 2026 · 2 revisions

An iterator that chains multiple iterable sources together into a single unified iterator.

This iterator SHALL accept any number of iterable values (arrays, Traversables, or Iterators) and iterate over them in order. When the current iterator is exhausted, it proceeds to the next.

The class MUST ensure all incoming values are wrapped as \Iterator instances, either natively or by converting Traversables or arrays using standard SPL iterators.

Example usage:

$it = new ChainIterableIterator([1, 2], new ArrayIterator([3, 4]));
foreach ($it as $value) {
    echo $value;
}
// Output: 1234

  • Full name: \FastForward\Iterator\ChainIterableIterator
  • This class is marked as final and can't be subclassed
  • This class implements: Iterator, Countable
  • This class is a Final class

Methods

__construct

Constructs a ChainIterableIterator with one or more iterable sources.

public __construct(iterable $iterables): mixed

Each iterable SHALL be normalized to a \Iterator instance using:

  • \ArrayIterator for arrays
  • \IteratorIterator for Traversable objects
  • Directly used if already an \Iterator

Parameters:

Parameter Type Description
$iterables iterable One or more iterable data sources to chain.

count

Counts the total number of elements across all chained iterators.

public count(): int

This method iterates through each underlying iterator and sums their counts.

Return Value:

the total count of elements in all chained iterators


rewind

Rewinds all underlying iterators and resets the position.

public rewind(): void

Each chained iterator SHALL be rewound to its beginning.


valid

Checks whether the current position is valid across chained iterators.

public valid(): bool

Iteration continues until the current iterator is valid or all are exhausted.

Return Value:

TRUE if there are more elements to iterate; FALSE otherwise


current

Returns the current element from the active iterator.

public current(): mixed|null

Return Value:

the current element or NULL if iteration is invalid


key

public key(): int|null

next

Moves the pointer of the active iterator forward.

public next(): void

Clone this wiki locally