Advertisement
fruffl

traits & collisions

Nov 29th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.83 KB | None | 0 0
  1. NAMESPACE Bob
  2. trait testTrait
  3. {
  4.     protected int _foo = 0;
  5. }
  6. NAMESPACE Bla
  7. trait anotherTrait
  8. {
  9.     protected int _foo = 5;
  10. }
  11. NAMESPACE Baz
  12. class baz
  13. {
  14.     use \Bob\testTrait, \Bla\anotherTrait; <- collision @ _foo
  15. }
  16.  
  17.  
  18. solution:
  19.     NAMESPACE ILLI\Base\Component\Delegate\Hook;
  20.     TRAIT tAdapter
  21.     {
  22.         protected $__Base_Component_Delegate_Hook_tAdapter_registry = [];
  23.        
  24.         protected function Base_Component_Delegate_Hook_tAdapter_defineInvoke($__triggerMethod, $__targetFunction, $__Instance, $__localScope = FALSE)
  25.         {
  26.             $this->Base_Component_Delegate_Hook_tAdapter_register($__triggerMethod, [$__Instance, $__targetFunction], $__localScope);
  27.             return $this;
  28.         }
  29.        
  30.         protected function Base_Component_Delegate_Hook_tAdapter_defineDelegate($__triggerMethod, $__targetFunction, $__Instance, $__localScope = FALSE)
  31.         {
  32.             $config             = new sConfigConstructMethod;
  33.             $config->Context        = $__Instance;
  34.             $config->functionName       = $__targetFunction;
  35.            
  36.             $adapter            = new sConcreteAdapter;
  37.             $adapter->triggerMethodName = $__triggerMethod;
  38.             $adapter->Delegate      = new Method($config);
  39.            
  40.             $this->Base_Component_Delegate_Hook_tAdapter_register($adapter->triggerMethodName, $adapter->Delegate, $__localScope);
  41.            
  42.             return $this;
  43.         }
  44.        
  45.         protected function Base_Component_Delegate_Hook_tAdapter_defineConcrete(sConcreteAdapter $__Adapter, $__localScope = FALSE)
  46.         {
  47.             $this->Base_Component_Delegate_Hook_tAdapter_register($__Adapter->triggerMethodName, $__Adapter->Delegate, $__localScope);
  48.            
  49.             return $this;
  50.         }
  51.        
  52.         protected function Base_Component_Delegate_Hook_tAdapter_register($__triggerMethod, $__delegate, $__localScope)
  53.         {
  54.             $__triggerMethod = (TRUE === $__localScope ? __CLASS__ : get_called_class()).'::'.$__triggerMethod;
  55.             $this->__Base_Component_Delegate_Hook_tAdapter_registry[$__triggerMethod] = $__delegate;
  56.             return $this;
  57.         }
  58.        
  59.         protected function Base_Component_Delegate_Hook_tAdapter_unregister($__triggerMethod)
  60.         {
  61.             if(isset($this->__Base_Component_Delegate_Hook_tAdapter_registry[$__triggerMethod]))
  62.                 unset($this->__Base_Component_Delegate_Hook_tAdapter_registry[$__triggerMethod]);
  63.                
  64.             return $this;
  65.         }
  66.        
  67.         protected function Base_Component_Delegate_Hook_tAdapter_exists($__triggerMethod)
  68.         {
  69.             return isset($this->__Base_Component_Delegate_Hook_tAdapter_registry[$__triggerMethod]);
  70.         }
  71.        
  72.         protected function Base_Component_Delegate_Hook_tAdapter_emit($__triggerMethod, array $__parameters = [])
  73.         {
  74.             if(FALSE === $this->Base_Component_Delegate_Hook_tAdapter_exists($__triggerMethod))
  75.                 return NULL;
  76.            
  77.             $Delegate = & $this->__Base_Component_Delegate_Hook_tAdapter_registry[$__triggerMethod];
  78.            
  79.             return (TRUE === ($Delegate instanceOf aMethodDelegate))
  80.                 ? $Delegate->emit($__parameters)
  81.                 : Invoke::callMethod($Delegate[0], $Delegate[1], $__parameters);;
  82.         }
  83.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement