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.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
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 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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.33, 8.0.0 - 8.0.30
rewind 1 => 1 2 => 2 rewind 3 => 3 4 => 4 5 => 5

preferences:
171.58 ms | 402 KiB | 209 Q