--- url: "https://refactoring.guru/design-patterns/iterator/php/example" titulek: "Iterator in PHP / Design Patterns" h1: "Iterator in PHP" popis: "Iterator pattern in PHP. Full code example in PHP with detailed comments and explanation. Iterator is a behavioral design pattern that allows sequential traversal through a complex data structure without exposing its internal details." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/iterator/php/example" drobecky: ["Home", "Design Patterns", "Iterator", "PHP"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:29:18" znaku: 6871 slov: 1028 kodovani: "utf-8" sha256: "dc6e06ea5a328512ee25230b6925a0de11fc362f44c054e001e5419b348033fb" --- # **Iterator** in PHP **Iterator** is a behavioral design pattern that allows sequential traversal through a complex data structure without exposing its internal details. Thanks to the Iterator, clients can go over elements of different collections in a similar fashion using a single iterator interface. [Learn more about Iterator](/design-patterns/iterator) **Complexity:** **Popularity:** **Usage examples:** The pattern is very common in PHP code. Many frameworks and libraries use it to provide a standard way for traversing their collections. PHP has a built-in [Iterator](/design-patterns/iterator) interface that can be used for building custom iterators compatible with the rest of the PHP code. **Identification:** Iterator is easy to recognize by the navigation methods (such as `next`, `previous` and others). Client code that uses iterators might not have direct access to the collection being traversed. ## Conceptual Example This example illustrates the structure of the **Iterator** design pattern and focuses on the following questions: - What classes does it consist of? - What roles do these classes play? - In what way the elements of the pattern are related? After learning about the pattern’s structure it’ll be easier for you to grasp the following example, based on a real-world PHP use case. #### **index.php:** Conceptual example ``` collection = $collection; $this->reverse = $reverse; } public function rewind() { $this->position = $this->reverse ? count($this->collection->getItems()) - 1 : 0; } public function current() { return $this->collection->getItems()[$this->position]; } public function key() { return $this->position; } public function next() { $this->position = $this->position + ($this->reverse ? -1 : 1); } public function valid() { return isset($this->collection->getItems()[$this->position]); } } /** * Concrete Collections provide one or several methods for retrieving fresh * iterator instances, compatible with the collection class. */ class WordsCollection implements \IteratorAggregate { private $items = []; public function getItems() { return $this->items; } public function addItem($item) { $this->items[] = $item; } public function getIterator(): Iterator { return new AlphabeticalOrderIterator($this); } public function getReverseIterator(): Iterator { return new AlphabeticalOrderIterator($this, true); } } /** * The client code may or may not know about the Concrete Iterator or Collection * classes, depending on the level of indirection you want to keep in your * program. */ $collection = new WordsCollection(); $collection->addItem("First"); $collection->addItem("Second"); $collection->addItem("Third"); echo "Straight traversal:\n"; foreach ($collection->getIterator() as $item) { echo $item . "\n"; } echo "\n"; echo "Reverse traversal:\n"; foreach ($collection->getReverseIterator() as $item) { echo $item . "\n"; } ``` #### **Output.txt:** Execution result ``` Straight traversal: First Second Third Reverse traversal: Third Second First ``` ## Real World Example Since PHP already has a built-in **Iterator** interface, which provides convenient integration with foreach loops, it’s very easy to create your own iterators for traversing almost every imaginable data structure. This example of the Iterator pattern provides easy access to CSV files. #### **index.php:** Real world example ``` filePointer = fopen($file, 'rb'); $this->delimiter = $delimiter; } catch (\Exception $e) { throw new \Exception('The file "' . $file . '" cannot be read.'); } } /** * This method resets the file pointer. */ public function rewind(): void { $this->rowCounter = 0; rewind($this->filePointer); // Read the first row to initialize $this->currentElement = fgetcsv($this->filePointer, self::ROW_SIZE, $this->delimiter); } /** * This method returns the current CSV row as a 2-dimensional array. * * @return array The current CSV row as a 2-dimensional array. */ public function current(): array { return $this->currentElement ?: []; } /** * This method returns the current row number. * * @return int The current row number. */ public function key(): int { return $this->rowCounter; } /** * This method moves to the next element. */ public function next(): void { if (is_resource($this->filePointer)) { $this->currentElement = fgetcsv($this->filePointer, self::ROW_SIZE, $this->delimiter); $this->rowCounter++; } } /** * This method checks if the current position is valid. * * @return bool If the current position is valid. */ public function valid(): bool { if ($this->currentElement === false) { if (is_resource($this->filePointer)) { fclose($this->filePointer); } return false; } return is_resource($this->filePointer); } } /** * The client code. */ $csv = new CsvIterator(__DIR__ . '/cats.csv'); foreach ($csv as $key => $row) { print_r($row); } ``` #### **Output.txt:** Execution result ``` Array ( [0] => Name [1] => Age [2] => Owner [3] => Breed [4] => Image [5] => Color [6] => Texture [7] => Fur [8] => Size ) Array ( [0] => Steve [1] => 3 [2] => Alexander Shvets [3] => Bengal [4] => /cats/bengal.jpg [5] => Brown [6] => Stripes [7] => Short [8] => Medium ) Array ( [0] => Siri [1] => 2 [2] => Alexander Shvets [3] => Domestic short-haired [4] => /cats/domestic-sh.jpg [5] => Black [6] => Solid [7] => Medium [8] => Medium ) Array ( [0] => Fluffy [1] => 5 [2] => John Smith [3] => Maine Coon [4] => /cats/Maine-Coon.jpg [5] => Gray [6] => Stripes [7] => Long [8] => Large ) ``` ## **Iterator** in Other Languages