Content · Web Development · PHP

PHP InternalIterator::rewind() Method: Usage and Examples

PHP InternalIterator::rewind() Method: Usage and Examples: Restarts traversal when supported by the wrapped iterator.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

An iterator has a current position and advances only when traversal is requested.

What Is InternalIterator::rewind in PHP?


Restarts traversal when supported by the wrapped iterator.

InternalIterator::rewind() : void

This operation does not accept method arguments.

Return type: void. Rewinding is not supported equally by every Traversable source, especially consumed generators.

Example 1: Understanding the Basic Behavior


<?php

$source = new SplFixedArray(3);
$source[0] = 10;
$source[1] = 20;
$source[2] = 30;
$iterator = $source->getIterator();
$iterator->rewind();
$iterator->rewind();
var_dump($iterator->current());

Output:

int(10)

Explanation: Restarts traversal when supported by the wrapped iterator.

Example 2: Testing Another Case


<?php

$source = new SplFixedArray(3);
$source[0] = 15;
$source[1] = 25;
$source[2] = 35;
$iterator = $source->getIterator();
$iterator->rewind();
$iterator->rewind();
var_dump($iterator->current());

Output:

int(15)

Explanation: Rewinding is not supported equally by every Traversable source, especially consumed generators.

Practical Example: Applying the Operation


<?php

$source = new SplFixedArray(3);
$source[0] = 40;
$source[1] = 50;
$source[2] = 60;
$iterator = $source->getIterator();
$iterator->rewind();
$iterator->rewind();
var_dump($iterator->current());

Output:

int(40)

Explanation: Most iterator methods depend on cursor position, and behavior after exhaustion depends on the underlying iterator.

Important Behavior and Edge Cases


  • Rewinding is not supported equally by every Traversable source, especially consumed generators.
  • Most iterator methods depend on cursor position, and behavior after exhaustion depends on the underlying iterator.

Comparison With Related Approaches


ApproachWhen to Use It
InternalIterator::rewindRestarts traversal when supported by the wrapped iterator.
Iterator interfaceArrayIterator wraps an array; iterator decorators can modify traversal without copying the source values.

Common Beginner Mistakes


MistakeCorrect Approach
Assuming every wrapped iterator can restart, even after a generator has advanced.Rewinding is not supported equally by every Traversable source, especially consumed generators.
Choosing Iterator interface without checking whether it provides the same result.ArrayIterator wraps an array; iterator decorators can modify traversal without copying the source values.

Key Takeaways


  • Restarts traversal when supported by the wrapped iterator.
  • Rewinding is not supported equally by every Traversable source, especially consumed generators.
  • Most iterator methods depend on cursor position, and behavior after exhaustion depends on the underlying iterator.

The examples demonstrate where InternalIterator::rewind fits within traversable collections. Verify the relevant preconditions whenever you adapt the code to real data.

Share this resourceSend this page to someone who may find it useful.