Last active
March 31, 2016 18:16
-
-
Save drgomesp/2fa59047084e96019e82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Item | |
{ | |
public $name; | |
public $parent; | |
public function __construct($name, Item $parent = null) | |
{ | |
$this->name = $name; | |
$this->parent = $parent; | |
} | |
} | |
function render(Item $item) | |
{ | |
yield $item; | |
if ($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) { | |
echo $item->name . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment