3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Item { public $name; public $parent; public function __construct($name, $parent = null) { $this->name = $name; $this->parent = $parent; } } function render(Item $item) { yield $item; if (is_object($item) && $item->parent) { yield from render($item->parent); } } $a = new Item('a'); $b = new Item('b', $a); $c = new Item('c', $b); $d = new Item('d', $c); $e = new Item('e', $d); foreach (render($e) as $item) { var_dump($item); }
Output for 7.0.0 - 7.2.0
object(Item)#5 (2) { ["name"]=> string(1) "e" ["parent"]=> object(Item)#4 (2) { ["name"]=> string(1) "d" ["parent"]=> object(Item)#3 (2) { ["name"]=> string(1) "c" ["parent"]=> object(Item)#2 (2) { ["name"]=> string(1) "b" ["parent"]=> object(Item)#1 (2) { ["name"]=> string(1) "a" ["parent"]=> NULL } } } } } object(Item)#4 (2) { ["name"]=> string(1) "d" ["parent"]=> object(Item)#3 (2) { ["name"]=> string(1) "c" ["parent"]=> object(Item)#2 (2) { ["name"]=> string(1) "b" ["parent"]=> object(Item)#1 (2) { ["name"]=> string(1) "a" ["parent"]=> NULL } } } } object(Item)#3 (2) { ["name"]=> string(1) "c" ["parent"]=> object(Item)#2 (2) { ["name"]=> string(1) "b" ["parent"]=> object(Item)#1 (2) { ["name"]=> string(1) "a" ["parent"]=> NULL } } } object(Item)#2 (2) { ["name"]=> string(1) "b" ["parent"]=> object(Item)#1 (2) { ["name"]=> string(1) "a" ["parent"]=> NULL } } object(Item)#1 (2) { ["name"]=> string(1) "a" ["parent"]=> NULL }
Output for 5.5.24 - 5.6.28
Parse error: syntax error, unexpected 'render' (T_STRING) in /in/MlYOb on line 21
Process exited with code 255.
Output for 5.4.10 - 5.4.45
Parse error: syntax error, unexpected '$item' (T_VARIABLE) in /in/MlYOb on line 18
Process exited with code 255.
Output for 5.3.20 - 5.3.29
Parse error: syntax error, unexpected T_VARIABLE in /in/MlYOb on line 18
Process exited with code 255.