The PHP FilterIterator::next API performs a focused operation on an object or value. The examples below show its effect on actual data.
Advances to the next iteration position; an exhausted iterator has no current entry.
What Is FilterIterator::next in PHP?
Advances to the next iteration position; an exhausted iterator has no current entry.
Syntax:
FilterIterator::next()
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->rewind();
$filterIterator->next();
var_dump($filterIterator->current());
Output:
int(12)
Advances to the next iteration position; an exhausted iterator has no current entry.
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->rewind();
$filterIterator->next();
var_dump($filterIterator->current());
Output:
int(18)
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->rewind();
$filterIterator->next();
var_dump($filterIterator->current());
Output:
int(30)
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.
- Advances to the next iteration position; an exhausted iterator has no current entry.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
FilterIterator::next | Advances to the next iteration position; an exhausted iterator has no current entry. |
| 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::next | 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
- Advances to the next iteration position; an exhausted iterator has no current entry.
- 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::next 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.