0
0
PHPprogramming~5 mins

Iterator interface implementation in PHP

Choose your learning style9 modes available
Introduction

We use the Iterator interface to make our objects easy to loop over with foreach. It helps us control how the object gives out its items one by one.

When you want to loop through a custom collection of items in your object.
When you want to control the order or way items are accessed in a loop.
When you want your object to work like an array in a foreach loop.
When you want to hide the internal structure of your data but still allow looping.
When you want to add extra logic while looping through items.
Syntax
PHP
<?php
class MyIterator implements Iterator {
    public function current() {}
    public function key() {}
    public function next() {}
    public function rewind() {}
    public function valid() {}
}
?>

You must define all five methods: current(), key(), next(), rewind(), and valid().

These methods control how the loop moves through your data.

Examples
This example shows a simple iterator over a fixed array of numbers.
PHP
<?php
class Numbers implements Iterator {
    private array $numbers = [1, 2, 3];
    private int $position = 0;

    public function __construct() {
        $this->position = 0;
    }

    public function current() {
        return $this->numbers[$this->position];
    }

    public function key() {
        return $this->position;
    }

    public function next() {
        $this->position++;
    }

    public function rewind() {
        $this->position = 0;
    }

    public function valid() {
        return isset($this->numbers[$this->position]);
    }
}
?>
This example uses isset() to check if the current position is valid.
PHP
<?php
class Letters implements Iterator {
    private array $letters = ['a', 'b', 'c'];
    private int $index = 0;

    public function current() {
        return $this->letters[$this->index];
    }

    public function key() {
        return $this->index;
    }

    public function next() {
        $this->index++;
    }

    public function rewind() {
        $this->index = 0;
    }

    public function valid() {
        return isset($this->letters[$this->index]);
    }
}
?>
Sample Program

This program creates a DaysOfWeek object that can be looped with foreach. It prints each day with its position.

PHP
<?php
class DaysOfWeek implements Iterator {
    private array $days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
    private int $position = 0;

    public function __construct() {
        $this->position = 0;
    }

    public function current() {
        return $this->days[$this->position];
    }

    public function key() {
        return $this->position;
    }

    public function next() {
        $this->position++;
    }

    public function rewind() {
        $this->position = 0;
    }

    public function valid() {
        return isset($this->days[$this->position]);
    }
}

$week = new DaysOfWeek();

foreach ($week as $key => $day) {
    echo "$key: $day\n";
}
?>
OutputSuccess
Important Notes

Remember to reset your position in rewind() so the loop starts from the beginning.

valid() should return true if the current position is valid, false otherwise.

Using isset() in valid() is a simple way to check if the current item exists.

Summary

The Iterator interface lets your object be used in foreach loops.

You must implement five methods to control the loop.

This helps you make custom collections easy and safe to loop through.