A classe NoRewindIterator

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

Introdução

Este iterador ignora operações de rebobinamento. Isso permite processar um iterador em vários loops foreach parciais.

Resumo da classe

class NoRewindIterator extends IteratorIterator {
/* Métodos */
public function __construct(Iterator $iterator)
public function current(): mixed
public function key(): mixed
public function next(): void
public function rewind(): void
public function valid(): bool
/* Métodos herdados */
public function IteratorIterator::current(): mixed
public function IteratorIterator::key(): mixed
public function IteratorIterator::next(): void
public function IteratorIterator::rewind(): void
public function IteratorIterator::valid(): bool
}

Índice

adicionar nota

Notas de Usuários 1 note

up
5
Anonymous
6 years ago
As its name implies, NoRewindIterator doesn't invoke the "rewind" method when It reaches the end of the iterator.

Let's demonstrate it by two examles.

In this example the "rewind" method will be invoked after when the "foreache" reaches its end, so, we can repeat printing the same values as many times as we want:

<?PHP
$iterator = new ArrayIterator(['PHP', 'Python', 'Go']);

foreach ($iterator as $item) {
    echo $item.PHP_EOL;
}

foreach ($iterator as $item) {
    echo $item.PHP_EOL;
}
?>

By using the NoRewindIterator, the "rewind" won't be invoked, so, we can't do as we did in previous example:

<?PHP
$iterator = new ArrayIterator(['PHP', 'Python', 'Go']);
$iterator = new NoRewindIterator($iterator);

foreach ($iterator as $item) {
    echo $item.PHP_EOL;
}

// doesn't do anything
foreach ($iterator as $item) {
    echo $item.PHP_EOL;
}

?>
To Top