Advertisement
fruffl

(de-)reference|link/unlink

Jan 10th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.11 KB | None | 0 0
  1. <?PHP
  2.  
  3.     CLASS Object
  4.     {
  5.         public function __construct(array $__options = [])
  6.         {
  7.             $__options += [];
  8.             $this->__runtimeConfig = $__options;
  9.             if(TRUE === $this->__initConfig['__run'])
  10.                 $this->__configure();
  11.                
  12.             return $this; // important for class::__construct(), see link()!
  13.         }
  14.  
  15.         protected function __configure()
  16.         {  
  17.             foreach($this->__runtimeConfig as $property => &$value)
  18.             {
  19.                 $this->{$property} =& $value;       // note: reference to property
  20.                 unset($this->__initConfig[$property]);
  21.             }
  22.            
  23.             return $this;
  24.         }
  25.        
  26.         /**
  27.          * create new object from called-class with referenced runtimeConfig
  28.          */
  29.         public function link()
  30.         {
  31.             $class  = get_called_class();
  32.             return $class::__construct($this->__runtimeConfig); // rebuild
  33.         }
  34.        
  35.         /**
  36.          * create new object from called-class with dereferenced runtimeConfig
  37.          */
  38.         public function copy()
  39.         {
  40.             $class = get_called_class();
  41.             $config = $this->__runtimeConfig;   // config is a reference!
  42.             unset($this->__runtimeConfig);      // unlink
  43.             $this->__runtimeConfig = $config;   // re-reference from referenced properties
  44.             return new $class($config);     // build
  45.         }
  46.  
  47.         public function __clone()
  48.         {
  49.             // switch by iFace
  50.             if($this instanceOf iLink)
  51.                 return $this->link();
  52.             if($this instanceOf iCopy)
  53.                 return $this->copy();
  54.  
  55.             throw new \Exception('Not supported');
  56.         }
  57.     }
  58.  
  59.  
  60. // Test
  61.  
  62.     class bar EXTENDS \ILLI\Core\Collection {};
  63.    
  64.     $a = new bar(['data' => ['bar' => 'lol']]);
  65.     $l = $a->link();
  66.     $c = $a->copy();
  67.    
  68.    
  69.     //$baz = new baz(['data' => ['baz' => new \StdClass, 'bala' => 'foo']]);
  70.    
  71.     $aA = $a->toArray();
  72.     $lA = $l->toArray();
  73.     $cA = $c->toArray();
  74.     var_dump(compact('aA', 'lA', 'cA'));
  75.    
  76.     /*
  77.     array(3) {
  78.       ["aA"]=>
  79.       array(1) {
  80.         ["bar"]=>
  81.         string(3) "lol"
  82.       }
  83.       ["lA"]=>
  84.       array(1) {
  85.         ["bar"]=>
  86.         string(3) "lol"
  87.       }
  88.       ["cA"]=>
  89.       array(1) {
  90.         ["bar"]=>
  91.         string(3) "lol"
  92.       }
  93.     }
  94.     */
  95.    
  96.    
  97.     $a['bar'] = 'new lol';
  98.    
  99.     $aA = $a->toArray();
  100.     $lA = $l->toArray();
  101.     $cA = $c->toArray();
  102.     var_dump(compact('aA', 'lA', 'cA'));
  103.    
  104.     /*
  105.     array(3) {
  106.       ["aA"]=>
  107.       array(1) {
  108.         ["bar"]=>
  109.         string(7) "new lol"
  110.       }
  111.       ["lA"]=>
  112.       array(1) {
  113.         ["bar"]=>
  114.         string(7) "new lol"
  115.       }
  116.       ["cA"]=>
  117.       array(1) {
  118.         ["bar"]=>
  119.         string(3) "lol"
  120.       }
  121.     }
  122.     */
  123.    
  124.     $l['bar'] = 'new link';
  125.    
  126.     $aA = $a->toArray();
  127.     $lA = $l->toArray();
  128.     $cA = $c->toArray();
  129.     var_dump(compact('aA', 'lA', 'cA'));
  130.    
  131.     /*
  132.     array(3) {
  133.       ["aA"]=>
  134.       array(1) {
  135.         ["bar"]=>
  136.         string(8) "new link"
  137.       }
  138.       ["lA"]=>
  139.       array(1) {
  140.         ["bar"]=>
  141.         string(8) "new link"
  142.       }
  143.       ["cA"]=>
  144.       array(1) {
  145.         ["bar"]=>
  146.         string(3) "lol"
  147.       }
  148.     }
  149.     */
  150.    
  151.     $c['bar'] = 'new copy';
  152.    
  153.     $aA = $a->toArray();
  154.     $lA = $l->toArray();
  155.     $cA = $c->toArray();
  156.     var_dump(compact('aA', 'lA', 'cA'));
  157.    
  158.     /*
  159.     array(3) {
  160.       ["aA"]=>
  161.       array(1) {
  162.         ["bar"]=>
  163.         string(8) "new link"
  164.       }
  165.       ["lA"]=>
  166.       array(1) {
  167.         ["bar"]=>
  168.         string(8) "new link"
  169.       }
  170.       ["cA"]=>
  171.       array(1) {
  172.         ["bar"]=>
  173.         string(8) "new copy"
  174.       }
  175.     }
  176.     */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement