The PHP FilterIterator::rewind API performs a focused operation on an object or value. The examples below show its effect on actual data.
Moves the iterator cursor back to its initial traversable position.
What Is FilterIterator::rewind in PHP?
Moves the iterator cursor back to its initial traversable position.
Syntax:
FilterIterator::rewind()
This operation has no parameters.
Return type declared by PHP: mixed / not explicitly declared. The value or side effect is demonstrated in the examples below.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
class MinimumFilter extends FilterIterator {
public function accept(): bool { return $this->current() >= 10; }
}
$filterIterator = new MinimumFilter(new ArrayIterator([3, 10, 12, 15]));
$filterIterator->next();
$filterIterator->rewind();
var_dump($filterIterator->current());
Output:
int(10)
Moves the iterator cursor back to its initial traversable position.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
class MinimumFilter extends FilterIterator {
public function accept(): bool { return $this->current() >= 11; }
}
$filterIterator = new MinimumFilter(new ArrayIterator([1, 5, 11, 18]));
$filterIterator->next();
$filterIterator->rewind();
var_dump($filterIterator->current());
Output:
int(11)
The predicate should be inexpensive and free of side effects, because iteration can call accept() repeatedly.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
class MinimumFilter extends FilterIterator {
public function accept(): bool { return $this->current() >= 20; }
}
$filterIterator = new MinimumFilter(new ArrayIterator([0, 20, 30, 40]));
$filterIterator->next();
$filterIterator->rewind();
var_dump($filterIterator->current());
Output:
int(20)
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- The predicate should be inexpensive and free of side effects, because iteration can call accept() repeatedly.
- Moves the iterator cursor back to its initial traversable position.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
FilterIterator::rewind | Moves the iterator cursor back to its initial traversable position. |
| CallbackFilterIterator | FilterIterator requires a subclass that implements accept(); CallbackFilterIterator accepts a filter callback. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of FilterIterator::rewind | The predicate should be inexpensive and free of side effects, because iteration can call accept() repeatedly. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | FilterIterator requires a subclass that implements accept(); CallbackFilterIterator accepts a filter callback. |
Key Takeaways
- Moves the iterator cursor back to its initial traversable position.
- The predicate should be inexpensive and free of side effects, because iteration can call accept() repeatedly.
- Use the example that matches the values, runtime version and dependencies in your application.
FilterIterator::rewind is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.