Advertisement
fruffl

object::shallowCopy

Sep 27th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.97 KB | None | 0 0
  1.  
  2.         function Core_Std_DefaultObject___trait_IDefaultObject_shallowCopy()
  3.         {
  4.             $A = new SPLReflectionObject($this);
  5.             $B = new SPLReflectionClass(static::class);
  6.             $C = $B->newInstanceWithoutConstructor();
  7.            
  8.             $propertiesA = $A->getProperties();
  9.             $propertiesB = $B->getProperties();
  10.            
  11.             foreach(array_keys($propertiesB) as $k)
  12.             {
  13.                 if($propertiesB[$k]->isStatic()) //! it's PHP... http://stackoverflow.com/questions/15783984/reflection-properties-filter
  14.                     continue;
  15.                
  16.                 $propertiesA[$k]->setAccessible(TRUE);
  17.                 $propertiesB[$k]->setAccessible(TRUE);
  18.                
  19.                 $value = $propertiesA[$k]->getValue($this);
  20.                 $value = $this->__Core_Std_DefaultObject___trait_IDefaultObject___copyRecursive($C, FALSE, $value);
  21.                
  22.                 $propertiesB[$k]->setValue($C, $value);
  23.             }
  24.            
  25.             return $C;
  26.         }
  27.  
  28.         private function __Core_Std_DefaultObject___trait_IDefaultObject___copyRecursive(IDefaultObject $__Context, $__isDeep = FALSE, $__data)
  29.         {
  30.             if($__data === $__Context)
  31.                 return $__data;
  32.            
  33.             if(is_array($__data))
  34.             {
  35.                 foreach($__data as $k => $value)
  36.                     $__data[$k] = $this->{__FUNCTION__}($__Context, $__isDeep, $value);
  37.                
  38.                 return $__data;
  39.             }
  40.            
  41.             if(FALSE === is_object($__data))
  42.                 return $__data;
  43.            
  44.             if(TRUE === $__isDeep)
  45.             {
  46.                 switch(TRUE):
  47.                     case $__data instanceOf IDefaultObject:
  48.                         $__data = $__data->deepCopy();
  49.                         break;
  50.                     case $__data instanceOf SPLClosure:
  51.                         $__data = $__data->bindTo($__Context, $__Context);
  52.                         break;
  53.                     case $__data instanceOf Fn:
  54.                         $__data = $__data->bind($__Context, $__Context);
  55.                         break;
  56.                     default:
  57.                         $__data = clone $__data;
  58.                 endswitch;
  59.             }
  60.             else
  61.             {
  62.                 switch(TRUE):
  63.                     case $__data instanceOf SPLClosure:
  64.                         $__data = $__data->bindTo($__Context, $__Context);
  65.                         break;
  66.                     case $__data instanceOf Fn:
  67.                         $__data = $__data->bind($__Context, $__Context);
  68.                         break;
  69.                 endswitch;
  70.             }
  71.            
  72.             return $__data;
  73.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement