https://www.php.net/manual/en/class.iteratoraggregate.php
https://www.php.net/manual/en/class.arrayiterator.php
class CarsIterator implements Iterator {
private $position = 0;
private $cars = ['BMW', 'Audi', 'KIA', 'Opel', 'Ford'];
public function current() {
return $this->cars[$this->position];
}
public function key() {
return $this->position;
}
public function next() {
$this->position++;
}
public function rewind() {
$this->position = 0;
}
public function valid() {
if (isset($this->cars[$this->position])) {
return true;
}
return false;
}
public function reverse() {
$this->cars = array_reverse($this->cars, true);
}
}
$tests = new CarsIterator();
var_dump($tests->current());
$tests->next();
$tests->next();
$tests->next();
$tests->next();
$tests->next();
var_dump($tests->current());