Iterators & IteratorAggregate
Also Known As
PHP Iterator
IteratorAggregate
Traversable
PHP SPL iterators
TL;DR
PHP interfaces that allow custom objects to be used in foreach loops, enabling lazy and memory-efficient iteration over any data source.
Explanation
PHP's Iterator interface requires five methods: current(), key(), next(), rewind(), valid(). IteratorAggregate requires just getIterator(), which returns an Iterator or Traversable — simpler for delegation. Custom iterators enable foreach over database result sets (fetching row by row), API paginated responses, filesystem trees, and infinite sequences — all with O(1) memory. PHP's SPL provides useful built-in iterators: DirectoryIterator, FilesystemIterator, RecursiveIteratorIterator, ArrayIterator, and LimitIterator. Generators (yield) are often a simpler alternative to manual Iterator implementation.
Common Misconception
✗ PHP iterators are only useful for custom data structures. Iterators enable memory-efficient processing of large datasets, lazy evaluation pipelines, and make any object usable in foreach — they are particularly valuable for streaming database results without loading everything into memory.
Why It Matters
PHP's Iterator and IteratorAggregate interfaces enable objects to be traversed with foreach — custom iterators allow lazy loading, filtering, and transformation without loading full datasets into memory.
Common Mistakes
- Implementing Iterator but forgetting rewind() — the iterator works once then is silently exhausted.
- Not implementing IteratorAggregate when you just need to wrap an existing array or Traversable — it's simpler.
- Returning false from current() on an empty iterator instead of returning null — both work but false is less predictable.
- Not using generators (yield) for lazy sequences — they automatically implement the Iterator protocol.
Code Examples
✗ Vulnerable
// Broken Iterator — missing rewind:
class NumberRange implements Iterator {
private int $current = 0;
public function __construct(private int $start, private int $end) {
$this->current = $start;
}
public function current(): int { return $this->current; }
public function key(): int { return $this->current - $this->start; }
public function next(): void { $this->current++; }
public function valid(): bool { return $this->current <= $this->end; }
// Missing: public function rewind(): void { $this->current = $this->start; }
}
✓ Fixed
// Implement Iterator to make a class foreach-able
class NumberRange implements Iterator {
private int \$current;
public function __construct(private int \$start, private int \$end) {
\$this->current = \$start;
}
public function current(): int { return \$this->current; }
public function key(): int { return \$this->current - \$this->start; }
public function next(): void { \$this->current++; }
public function rewind(): void { \$this->current = \$this->start; }
public function valid(): bool { return \$this->current <= \$this->end; }
}
foreach (new NumberRange(1, 3) as \$i) { echo \$i; } // 1 2 3
// IteratorAggregate — simpler approach
class UserCollection implements IteratorAggregate {
public function getIterator(): ArrayIterator {
return new ArrayIterator(\$this->users);
}
}
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
22
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Amazonbot 6
Unknown AI 2
Google 2
Ahrefs 2
ChatGPT 2
Perplexity 1
Also referenced
How they use it
crawler 13
crawler_json 2
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Medium
⚡ Quick Fix
Implement IteratorAggregate (simpler) rather than Iterator directly — return a generator from getIterator() and your class instantly works with foreach without implementing all 5 Iterator methods
📦 Applies To
PHP 5.0+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
Class with getItems() forcing callers to foreach($obj->getItems()); manual Iterator implementation when IteratorAggregate+generator would be simpler
Auto-detectable:
✗ No
phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Medium
Context: Class
Tests: Update