Last active
December 20, 2015 12:29
-
-
Save code-poel/6130996 to your computer and use it in GitHub Desktop.
Nested SPL objects with recursion.
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 Layout extends \ArrayIterator | |
{ | |
} | |
class Block extends \ArrayIterator | |
{ | |
protected $_alias; | |
public function __construct( $alias = null ) | |
{ | |
if( is_null( $alias ) ) | |
{ | |
$alias = uniqid( 'Block_' ); | |
} | |
$this->_alias = $alias; | |
} | |
public function getAlias() | |
{ | |
return $this->_alias; | |
} | |
} | |
try | |
{ | |
$b1 = new Block( 'Base' ); | |
$b1_b1 = new Block( 'Base > 1st Child' ); | |
$b1_b2 = new Block( 'Base > 2nd Child' ); | |
$b1_b1_b1 = new Block( 'Base > 1st Child > 1st Child' ); | |
$b1_b1->append( $b1_b1_b1 ); | |
$b1->append( $b1_b1 ); | |
$b1->append( $b1_b2 ); | |
$layout = new Layout; | |
$layout->append( $b1 ); | |
$storage = new \RecursiveIteratorIterator( new \RecursiveArrayIterator( $layout ), RecursiveIteratorIterator::SELF_FIRST ); | |
foreach( $storage as $key => $value ) | |
{ | |
print_r( $value->getAlias() . PHP_EOL ); | |
} | |
} | |
catch( \Exception $e ) | |
{ | |
var_dump( $e->getMessage() ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment