The NoRewindIterator class

(PHP 5 >= 5.1.0, PHP 7)

소개

This iterator cannot be rewound.

클래스 개요

NoRewindIterator extends IteratorIterator {
/* 메소드 */
public __construct ( Iterator $iterator )
public mixed current ( void )
public iterator getInnerIterator ( void )
public mixed key ( void )
public void next ( void )
public void rewind ( void )
public bool valid ( void )
/* 상속된 메소드 */
public mixed IteratorIterator::current ( void )
public Traversable IteratorIterator::getInnerIterator ( void )
public scalar IteratorIterator::key ( void )
public void IteratorIterator::next ( void )
public void IteratorIterator::rewind ( void )
public bool IteratorIterator::valid ( void )
}

Table of Contents

add a note add a note

User Contributed Notes 1 note

up
1
Anonymous
3 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