The PHP FilterIterator::accept API performs a focused operation on an object or value. The examples below show its effect on actual data.
Decides whether the current entry passes a filter and should be exposed during iteration.
What Is FilterIterator::accept in PHP?
Decides whether the current entry passes a filter and should be exposed during iteration.
Syntax:
FilterIterator::accept()
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();
var_dump($filterIterator->accept());
Output:
bool(true)
Decides whether the current entry passes a filter and should be exposed during iteration.
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();
var_dump($filterIterator->accept());
Output:
bool(true)
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();
var_dump($filterIterator->accept());
Output:
bool(true)
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.
- Decides whether the current entry passes a filter and should be exposed during iteration.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
FilterIterator::accept | Decides whether the current entry passes a filter and should be exposed during iteration. |
| 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::accept | 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
- Decides whether the current entry passes a filter and should be exposed during iteration.
- 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::accept 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.