3v4l.org

run code in 300+ PHP versions simultaneously
<?php function consumeIterator($iterator){ echo "consuming\n"; foreach($iterator as $content); } function consumeIteratorBeforeDestruction(Iterator $iterator){ return new class($iterator) implements Iterator{ private $iterator; function __construct(Iterator $iterator){ $this->iterator = $iterator; } function __destruct(){ consumeIterator($this->iterator); } function key(){ return $this->iterator->key(); } function current(){ return $this->iterator->current(); } function valid(){ return $this->iterator->valid(); } function rewind(){ return $this->iterator->rewind(); } function next(){ return $this->iterator->next(); } }; } class baz { function test(){ $a = function(){ yield 1; echo "complete\n"; }; return consumeIteratorBeforeDestruction($a()); } } function test($baz){ $iterator = $baz->test(); } echo "baz\n"; test(new baz); echo "foo\n";
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
baz Deprecated: Return type of Iterator@anonymous::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/uRbhh on line 14 Deprecated: Return type of Iterator@anonymous::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/uRbhh on line 17 Deprecated: Return type of Iterator@anonymous::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/uRbhh on line 13 Deprecated: Return type of Iterator@anonymous::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/uRbhh on line 15 Deprecated: Return type of Iterator@anonymous::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/uRbhh on line 16 consuming complete foo
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
baz consuming complete foo
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.38
Parse error: syntax error, unexpected 'class' (T_CLASS) in /in/uRbhh on line 9
Process exited with code 255.

preferences:
158.02 ms | 402 KiB | 295 Q