Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- CLASS Object
- {
- public function __construct(array $__options = [])
- {
- $__options += [];
- $this->__runtimeConfig = $__options;
- if(TRUE === $this->__initConfig['__run'])
- $this->__configure();
- return $this; // important for class::__construct(), see link()!
- }
- protected function __configure()
- {
- foreach($this->__runtimeConfig as $property => &$value)
- {
- $this->{$property} =& $value; // note: reference to property
- unset($this->__initConfig[$property]);
- }
- return $this;
- }
- /**
- * create new object from called-class with referenced runtimeConfig
- */
- public function link()
- {
- $class = get_called_class();
- return $class::__construct($this->__runtimeConfig); // rebuild
- }
- /**
- * create new object from called-class with dereferenced runtimeConfig
- */
- public function copy()
- {
- $class = get_called_class();
- $config = $this->__runtimeConfig; // config is a reference!
- unset($this->__runtimeConfig); // unlink
- $this->__runtimeConfig = $config; // re-reference from referenced properties
- return new $class($config); // build
- }
- public function __clone()
- {
- // switch by iFace
- if($this instanceOf iLink)
- return $this->link();
- if($this instanceOf iCopy)
- return $this->copy();
- throw new \Exception('Not supported');
- }
- }
- // Test
- class bar EXTENDS \ILLI\Core\Collection {};
- $a = new bar(['data' => ['bar' => 'lol']]);
- $l = $a->link();
- $c = $a->copy();
- //$baz = new baz(['data' => ['baz' => new \StdClass, 'bala' => 'foo']]);
- $aA = $a->toArray();
- $lA = $l->toArray();
- $cA = $c->toArray();
- var_dump(compact('aA', 'lA', 'cA'));
- /*
- array(3) {
- ["aA"]=>
- array(1) {
- ["bar"]=>
- string(3) "lol"
- }
- ["lA"]=>
- array(1) {
- ["bar"]=>
- string(3) "lol"
- }
- ["cA"]=>
- array(1) {
- ["bar"]=>
- string(3) "lol"
- }
- }
- */
- $a['bar'] = 'new lol';
- $aA = $a->toArray();
- $lA = $l->toArray();
- $cA = $c->toArray();
- var_dump(compact('aA', 'lA', 'cA'));
- /*
- array(3) {
- ["aA"]=>
- array(1) {
- ["bar"]=>
- string(7) "new lol"
- }
- ["lA"]=>
- array(1) {
- ["bar"]=>
- string(7) "new lol"
- }
- ["cA"]=>
- array(1) {
- ["bar"]=>
- string(3) "lol"
- }
- }
- */
- $l['bar'] = 'new link';
- $aA = $a->toArray();
- $lA = $l->toArray();
- $cA = $c->toArray();
- var_dump(compact('aA', 'lA', 'cA'));
- /*
- array(3) {
- ["aA"]=>
- array(1) {
- ["bar"]=>
- string(8) "new link"
- }
- ["lA"]=>
- array(1) {
- ["bar"]=>
- string(8) "new link"
- }
- ["cA"]=>
- array(1) {
- ["bar"]=>
- string(3) "lol"
- }
- }
- */
- $c['bar'] = 'new copy';
- $aA = $a->toArray();
- $lA = $l->toArray();
- $cA = $c->toArray();
- var_dump(compact('aA', 'lA', 'cA'));
- /*
- array(3) {
- ["aA"]=>
- array(1) {
- ["bar"]=>
- string(8) "new link"
- }
- ["lA"]=>
- array(1) {
- ["bar"]=>
- string(8) "new link"
- }
- ["cA"]=>
- array(1) {
- ["bar"]=>
- string(8) "new copy"
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement