Content · Programming · PHP

Creating Custom Iterators

Creating Custom Iterators: Custom iterators implement the Iterator contract so an object can control its own traversal. See three examples and important limitations.

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

Custom iterators implement the Iterator contract so an object can control its own traversal.

The examples below begin with the core behavior, then move to an application-oriented use.

How Creating Custom Iterators Works


Custom iterators implement the Iterator contract so an object can control its own traversal.

Example 1: Basic Usage


This example uses a small input to make the main behavior visible.

<?php

$iterator = new ArrayIterator(["red", "blue"]);
$iterator->rewind();
while ($iterator->valid()) { echo $iterator->current() . "\n"; $iterator->next(); }

Output:

red
blue

Custom iterators implement the Iterator contract so an object can control its own traversal.

Example 2: A Different Input


A different set of values shows the same operation in another situation.

<?php

class PriceFilter extends FilterIterator { public function accept(): bool { return $this->current() > 10; } }
$iterator = new PriceFilter(new ArrayIterator([4, 12, 25]));
foreach ($iterator as $price) { echo $price . " "; }

Output:

12 25 

Keep cursor methods consistent; valid() must return false after the last element and rewind() must restore traversal.

Practical Example: Applying the Feature


This example shows an application-style use of the feature, with its surrounding setup visible.

<?php

class StepIterator implements Iterator { private int $position = 0; public function __construct(private array $items) {} public function rewind(): void { $this->position = 0; } public function current(): mixed { return $this->items[$this->position]; } public function key(): int { return $this->position; } public function next(): void { ++$this->position; } public function valid(): bool { return isset($this->items[$this->position]); } }
foreach (new StepIterator(["one", "two"]) as $item) { echo $item; }

Output:

onetwo

The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.

Important Behavior and Edge Cases


  • Keep cursor methods consistent; valid() must return false after the last element and rewind() must restore traversal.
  • Custom iterators implement the Iterator contract so an object can control its own traversal.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
Creating Custom IteratorsCustom iterators implement the Iterator contract so an object can control its own traversal.
Alternative approachIteratorAggregate is simpler when an existing iterator or generator can perform traversal.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of Creating Custom IteratorsKeep cursor methods consistent; valid() must return false after the last element and rewind() must restore traversal.
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 identicallyIteratorAggregate is simpler when an existing iterator or generator can perform traversal.

Key Takeaways


  • Custom iterators implement the Iterator contract so an object can control its own traversal.
  • Keep cursor methods consistent; valid() must return false after the last element and rewind() must restore traversal.
  • Use the example that matches the values, runtime version and dependencies in your application.

Creating Custom Iterators 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.