Content · Programming · PHP

PHP FilterIterator::accept() Method: Usage and Examples

PHP FilterIterator::accept() Method: Usage and Examples: Decides whether the current entry passes a filter and should be exposed during iteration. See three examples and important limitations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

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


ApproachImportant Difference
FilterIterator::acceptDecides whether the current entry passes a filter and should be exposed during iteration.
CallbackFilterIteratorFilterIterator requires a subclass that implements accept(); CallbackFilterIterator accepts a filter callback.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of FilterIterator::acceptThe predicate should be inexpensive and free of side effects, because iteration can call accept() repeatedly.
Expecting a different type of resultRead the declared return type and distinguish returned values from changes to the source object.
Assuming a related API works identicallyFilterIterator 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.

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