3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MyIter implements Iterator { private $a; private $key = 0; public function __construct(array $a) { $this->a = array_values($a); } public function key() { return $this->key; } public function rewind() { $this->key = 0; } public function valid() { echo __METHOD__, PHP_EOL; return isset($this->a[$this->key]); } public function next() { echo __METHOD__, PHP_EOL; $this->key++; } public function current() { echo __METHOD__, PHP_EOL; return $this->a[$this->key]; } } $ilist = new MyIter(array('A', 'B', 'C', 'D')); foreach ($ilist as $var) { echo "var = $var\n"; echo "current = ", print_r(current($ilist), true), "\n"; }

preferences:
45.11 ms | 402 KiB | 5 Q