In PHP, how do I iterate over objects with SPL?

PHP, SPL, Iterator, Object Iteration
This example illustrates how to iterate over objects using the Standard PHP Library.
<?php class MyIterator implements Iterator { private $data = []; private $index = 0; public function __construct($data) { $this->data = $data; } public function current() { return $this->data[$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->data[$this->index]); } } $data = ['first' => 'apple', 'second' => 'banana', 'third' => 'cherry']; $iterator = new MyIterator($data); foreach ($iterator as $key => $value) { echo "$key: $value<br>"; } ?>

PHP SPL Iterator Object Iteration