update pague now
PHP 8.5.2 Released!

The NoRewindIterator class

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

Introduction

This iterator ignores rewind operations. This allows processsing an iterator in multiple partial foreach loops.

Table of Contens

add a note

User Contributed Notes 1 note

Anonymous
5 years ago
As its name implies, NoRewindIterator doesn't invoque 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 invoqued 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 ($iteratoras$item) {
    echo$item.PHP_EOL;
}

foreach ($iteratoras$item) {
    echo$item.PHP_EOL;
}
?>
By using the NoRewindIterator, the "rewind" won't be invoqued, so, we can't do as we did in previous example:<?PHP
$iterator = new ArrayIterator(['PHP', 'Python', 'Go']);
$iterator= new NoRewindIterator($iterator);

foreach ($iteratoras$item) {
    echo$item.PHP_EOL;
}

// doesn't do anythingforeach ($iteratoras$item) {
    echo$item.PHP_EOL;
}

?>
To Top