Cloning object based on content in PHP -


i seem going round in circles here have situation when reading objects may come across contain array. when happens wish produce new objects based upon array example

sourceobject(namespace\classname)     protected '_var1' => array('value1', 'value2')     protected '_var2' => string 'variable 2' 

should become

childobject1(namespace\classname)     protected '_var1' => string 'value1'     protected '_var2' => string 'variable2'  childobject2(namespace\classname)     protected '_var1' => string 'value2'     protected '_var2' => string 'variable2' 

however due not quite getting head around clones end same content (sometimes both value1 value2)

you create method following one:

trait classsplitclone {     public function splitclone($name)     {          if (!is_array($this->$name)) {             return [$this];         }          $objs   = [];         $values = $this->$name;         $c      = 0;         foreach ($values $value) {             $objs[]          = $c ? clone $this : $this;             $objs[$c]->$name = $value;             $c++;         }          return $objs;     } } 

and use in class(es) trait

class classname {     use classsplitclone;      protected $var1;     protected $var2;      function __construct($var1, $var2)     {         $this->var1 = $var1;         $this->var2 = $var2;     } } 

an example like:

$obj = new classname(['value1', 'value2'], 'variable 2');  print_r($obj->splitclone('var1')); 

yielding following results:

array (     [0] => classname object         (             [var1:protected] => value1             [var2:protected] => variable 2         )      [1] => classname object         (             [var1:protected] => value2             [var2:protected] => variable 2         )  ) 

hope helps. alterantively can access private , protected members via reflectionobject.


the full example code (demo):

<?php /**  * @link http://stackoverflow.com/a/24110513/367456  */  /**  * class classsplitclone  */ trait classsplitclone {     public function splitclone($name)     {          if (!is_array($this->$name)) {             return [$this];         }          $objs   = [];         $values = $this->$name;         $c      = 0;         foreach ($values $value) {             $objs[]          = $c ? clone $this : $this;             $objs[$c]->$name = $value;             $c++;         }          return $objs;     } }  /**  * class classname  */ class classname {     use classsplitclone;      protected $var1;     protected $var2;      function __construct($var1, $var2)     {         $this->var1 = $var1;         $this->var2 = $var2;     } }  /*  * example  */ $obj = new classname(['value1', 'value2'], 'variable 2');  print_r($obj->splitclone('var1')); 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -