0
0
PHPprogramming~10 mins

Iterator interface implementation in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Iterator interface implementation
Create Iterator Object
Call rewind() to start
Check valid()
Get current()
Get key()
Move next()
Back to Check valid()
This flow shows how PHP's Iterator interface methods control looping: start with rewind(), then check valid(), get current() and key(), move next(), repeat until valid() is false.
Execution Sample
PHP
<?php
class MyIterator implements Iterator {
  private array $items = ["a", "b", "c"];
  private int $index = 0;
  public function rewind() { $this->index = 0; }
  public function current() { return $this->items[$this->index]; }
  public function key() { return $this->index; }
  public function next() { $this->index++; }
  public function valid() { return isset($this->items[$this->index]); }
}
$it = new MyIterator();
foreach ($it as $key => $value) {
  echo "$key:$value ";
}
?>
This code defines a class implementing Iterator to loop over an array and print keys and values.
Execution Table
StepMethod CalledIndex Valuevalid() ResultOutputNext Action
1rewind()0trueCheck valid()
2valid()0trueGet current() and key()
3current(), key()0true0:anext()
4next()1trueCheck valid()
5valid()1trueGet current() and key()
6current(), key()1true1:bnext()
7next()2trueCheck valid()
8valid()2trueGet current() and key()
9current(), key()2true2:cnext()
10next()3falseExit loop
11valid()3falseStop iteration
💡 Index reaches 3, valid() returns false because no item at index 3, loop ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
indexundefined00112233
Key Moments - 3 Insights
Why does the loop stop after index 3 even though the array has 3 items?
Because valid() checks if the current index exists in the array. At index 3, no item exists, so valid() returns false and the loop stops (see execution_table step 10).
What does rewind() do before the loop starts?
rewind() resets the index to 0 so iteration starts from the first item (see execution_table step 1).
Why do current() and key() get called after valid() returns true?
Because valid() confirms the current index is valid, then current() returns the item and key() returns the index for use in the loop (see execution_table steps 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the index value after the second call to next()?
A2
B3
C1
D0
💡 Hint
Check the 'index value' column at step 7 where next() is called the second time.
At which step does valid() first return false?
AStep 9
BStep 10
CStep 11
DStep 8
💡 Hint
Look at the 'valid() Result' column to find when it changes to false.
If rewind() set index to 1 instead of 0, what would be the first output key?
A0
B2
C1
DUndefined
💡 Hint
Check variable_tracker and execution_table step 1 for rewind() effect on index.
Concept Snapshot
Iterator interface requires these methods:
- rewind(): reset position
- current(): get current item
- key(): get current key
- next(): move forward
- valid(): check if current position is valid
PHP foreach calls these methods in this order to loop over objects implementing Iterator.
Full Transcript
This visual trace shows how PHP's Iterator interface works step-by-step. First, rewind() sets the index to 0. Then valid() checks if the current index exists in the array. If yes, current() returns the item and key() returns the index. Then next() moves the index forward. This repeats until valid() returns false, meaning no more items. The loop stops. The variable 'index' changes from undefined to 0 at rewind, then increments each next() call. This process controls how foreach loops over objects implementing Iterator.