3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo implements Iterator { public function rewind(){ echo "rewind\n"; } private $c = 0; public function valid(){ return $this->c++ < 5; } public function next(){} public function key(){ return $this->c; } public function current(){ return $this->c; } } $gen = (function(){ $foo = new Foo(); $foo->rewind(); $foo->valid(); yield $foo->key() => $foo->current(); $foo->next(); $foo->valid(); yield $foo->key() => $foo->current(); yield from $foo; })(); foreach($gen as $k => $v){ echo $k . " => " . $v . "\n"; }
Output for 8.1.0 - 8.1.7
Deprecated: Return type of Foo::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/8k4me on line 16 Deprecated: Return type of Foo::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/8k4me on line 14 Deprecated: Return type of Foo::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/8k4me on line 15 Deprecated: Return type of Foo::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/8k4me on line 10 Deprecated: Return type of Foo::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/8k4me on line 5 rewind 1 => 1 2 => 2 rewind 3 => 3 4 => 4 5 => 5
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.30, 8.0.0 - 8.0.20
rewind 1 => 1 2 => 2 rewind 3 => 3 4 => 4 5 => 5

preferences:
26.62 ms | 412 KiB | 5 Q