Skip to content

Instantly share code, notes, and snippets.

@drgomesp
Last active March 31, 2016 18:16
Show Gist options
  • Save drgomesp/2fa59047084e96019e82 to your computer and use it in GitHub Desktop.
Save drgomesp/2fa59047084e96019e82 to your computer and use it in GitHub Desktop.
<?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