State::getIterator()

The external iterator receiver.

Table of Contents
  1. Description
  2. Example

Description

State::getIterator(): Traversable;

This method is a requirement to be able to implement the IteratorAggregate interface and aims to make the object iterable directly. You won’t use this method directly, iteration control structures such as for, foreach and while will call this method automatically.

Example

$state = new State([
    'bar' => 1,
    'baz' => 2,
    'qux' => 3
]);

// Using `foreach` loop
foreach ($state as $k => $v) {
    echo $k . ': ' . $v . '<br>';
}

// Using `while` loop
$iterator = $state->getIterator();
while ($iterator->valid()) {
    echo $iterator->key() . ': ' . $iterator->current() . '<br>';
    $iterator->next();
}

State::getIterator()

The external iterator receiver.