<?php
class Counter implements Iterator
{
private $current = 0;
private $index = 0;
private $low = 0;
private $high = 0;
private $step = 0;
public function __construct($low, $high, $step) {
$this->current = $low;
$this->low = $low;
$this->high = $high;
$this->step = $step;
}
public function rewind() {
$this->current = $this->low;
$this->index = 0;
}
public function current() {
return $this->current;
}
public function key() {
return $this->index;
}
public function next() {
$this->index++;
return $this->current = $this->current + $this->step;
}
public function valid() {
return
$this->current <= $this->high &&
$this->current >= $this->low;
}
}
$counter = new Counter(3, 9, 2);
foreach ($counter as $item) {
echo $item . "\n";
}
//printed 3, 5, 7, 9