3v4l.org

run code in 300+ PHP versions simultaneously
<?php class lazyNew implements Iterator { private $obj = null; private $func ; public function __construct($func) { $this->func = $func; } public function init() { if ($this->obj === null) { $func = $this->func; $this->obj = $func(); } return $this->obj; } public function current() { return $this->init()->current(); } public function valid() { echo "valid on ".get_class($this)."\n"; //also try var_dump($this) return $this->init()->valid(); } public function next() { return $this->init()->next(); } public function key() { return $this->init()->key(); } public function rewind() { echo "rewind on ".get_class($this)."\n"; return $this->init()->rewind(); } } $P = new lazyNew(function() use (&$P) { return $P = new ArrayIterator(array(1,2,3,4,5)); }); foreach ($P as $val) echo($val); ?>
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Return type of lazyNew::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ZcpQZ on line 15 Deprecated: Return type of lazyNew::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ZcpQZ on line 23 Deprecated: Return type of lazyNew::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ZcpQZ on line 26 Deprecated: Return type of lazyNew::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ZcpQZ on line 18 Deprecated: Return type of lazyNew::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/ZcpQZ on line 29 rewind on lazyNew valid on lazyNew 1valid on lazyNew 2valid on lazyNew 3valid on lazyNew 4valid on lazyNew 5valid on lazyNew
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
rewind on lazyNew valid on lazyNew 1valid on lazyNew 2valid on lazyNew 3valid on lazyNew 4valid on lazyNew 5valid on lazyNew
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
rewind on lazyNew valid on ArrayIterator Fatal error: Call to undefined method ArrayIterator::init() in /in/ZcpQZ on line 21
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /in/ZcpQZ on line 36
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION, expecting ')' in /in/ZcpQZ on line 36
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/ZcpQZ on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/ZcpQZ on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/ZcpQZ on line 2
Process exited with code 255.

preferences:
194.87 ms | 401 KiB | 314 Q