3v4l.org

run code in 300+ PHP versions simultaneously
<?php function makeGen(array $array = []): Generator { $item = current($array); yield $item; while($item = next($array)) { yield $item; } } class GeneratorIterator implements \Iterator { private $genMaker; private $genArg; private $current; public function __construct(callable $genMaker, $arg) { $this->genMaker = $genMaker; $this->genArg = $arg; $this->current = $genMaker($arg); } public function current() { return $this->current->current(); } public function key() { return $this->current->key(); } public function rewind() { $this->current = call_user_func($this->genMaker, $this->genArg); } public function valid() { return $this->current->valid(); } public function next() { $this->current->next(); } } $genIterator = new GeneratorIterator('makeGen', [1,2,3]); foreach($genIterator as $key => $value) { echo $key . ' -> ' . $value . "\n"; } echo "\n"; foreach($genIterator as $key => $value) { echo $key . ' -> ' . $value . "\n"; }
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
Deprecated: Return type of GeneratorIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/QOEAs on line 26 Deprecated: Return type of GeneratorIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/QOEAs on line 46 Deprecated: Return type of GeneratorIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/QOEAs on line 31 Deprecated: Return type of GeneratorIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/QOEAs on line 41 Deprecated: Return type of GeneratorIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/QOEAs on line 36 0 -> 1 1 -> 2 2 -> 3 0 -> 1 1 -> 2 2 -> 3
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
0 -> 1 1 -> 2 2 -> 3 0 -> 1 1 -> 2 2 -> 3

preferences:
139.22 ms | 409 KiB | 5 Q