3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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 7.0.0 - 7.1.0
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.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.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.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.