3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class InfiniteNumbers implements Iterator { public $start; public $current; public function __construct($start) { $this->start = $start; $this->current = $start; } public function next() { echo "next called\n"; $this->current++; } public function current() { echo "current called\n"; return $this->current; } public function key() { echo "key called\n"; return $this->current; } public function rewind() { echo "rewind called\n"; $this->current = $this->start; } public function valid() { echo "valid called\n"; return true; } } $infinite = new InfiniteNumbers(1); var_dump($infinite); foreach($infinite as $number) { if($number > 10) { break; } echo $number."\n"; }
based on ElZfK
Output for 5.6.0 - 5.6.30, hhvm-3.12.14 - 3.17.3, 7.0.0 - 7.3.1
object(InfiniteNumbers)#1 (2) { ["start"]=> int(1) ["current"]=> int(1) } rewind called valid called current called 1 next called valid called current called 2 next called valid called current called 3 next called valid called current called 4 next called valid called current called 5 next called valid called current called 6 next called valid called current called 7 next called valid called current called 8 next called valid called current called 9 next called valid called current called 10 next called valid called current called