Advertisement
Guest User

Schirkan

a guest
Jan 9th, 2009
4,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.13 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package Multiple Inheritance
  4.  * @author Martin Pietschmann <schirkan86@gmx.de>
  5.  */
  6.  
  7. /**
  8.  * Inheritanced classes will have full access to
  9.  * the methods and attributes (public and protected)
  10.  * of the Base class they are registered with.
  11.  */
  12. class MI_Importable
  13. {
  14.     private $base = null;
  15.  
  16.     /**
  17.      * Registers this Object with the Base class
  18.      * @param Base $base
  19.      */
  20.     final public function register(MI_Base $base) {
  21.         $this->base = $base;
  22.     }
  23.  
  24.     /**
  25.      * Calls a method of $this->base
  26.      * @param string $method
  27.      * @param string $args
  28.      * @return mixed
  29.      */
  30.     final public function __call($method, $args) {
  31.         return call_user_func_array(array($this->base, $method), $args);
  32.     }
  33.  
  34.     /**
  35.      * @param string $var name of attribute
  36.      * @return mixed
  37.      */
  38.     final public function __get($var) {
  39.         return $this->base->__get_var($this, $var);
  40.     }
  41.  
  42.     /**
  43.      * @param string $var name of attribute
  44.      * @param mixed $value
  45.      */
  46.     final public function __set($var, $value) {
  47.         return $this->base->__set_var($this, $var, $value);
  48.     }
  49. }
  50.  
  51. class MI_Base
  52. {
  53.     private $imported_objects = array();
  54.     private $imported_functions = array();
  55.  
  56.     /**
  57.      * Import method
  58.      * @param MI_Importable $new_import
  59.      */
  60.     final protected function import(MI_Importable $new_import) {
  61.         $new_import->register($this);
  62.         $this->imported_objects[] = &$new_import;
  63.         // the new functions to import
  64.         $import_functions = get_class_methods(get_class($new_import));
  65.         $MI_Importable_functions = get_class_methods('MI_Importable');
  66.         foreach($import_functions as $function_name) {
  67.             if(in_array($function_name, $MI_Importable_functions)) continue;
  68.             if(isset($this->imported_functions[$function_name]))
  69.                 throw new Exception('Duplicated function name: '.$function_name);
  70.             $this->imported_functions[$function_name] = &$new_import;
  71.         }
  72.     }
  73.  
  74.     /**
  75.      * Calls a method
  76.      * @param string $method
  77.      * @param string $args
  78.      * @return mixed
  79.      */
  80.     final public function __call($method, $args) {
  81.         // make sure the function exists
  82.         if(isset($this->imported_functions[$method]))
  83.             return call_user_func_array(array($this->imported_functions[$method], $method), $args);
  84.         throw new Exception ('Call to undefined method/class function: ' . $method);
  85.     }
  86.  
  87.     /**
  88.      * Gets a public or protected attribute
  89.      * @param MI_Importable $caller
  90.      * @param string $var name of attribute
  91.      * @return mixed
  92.      */
  93.     final public function __get_var(MI_Importable $caller, $var) {
  94.         if(in_array($caller, $this->imported_objects)) return $this->{$var};
  95.         else throw new Exception('Unauthorized Access to "__get_var()".');
  96.     }
  97.  
  98.     /**
  99.      * Sets a public or protected attribute
  100.      * @param MI_Importable $caller
  101.      * @param string $var name of attribute
  102.      * @param mixed $value
  103.      */
  104.     final public function __set_var(MI_Importable $caller, $var, $value) {
  105.         if(in_array($caller, $this->imported_objects)) $this->{$var} = $value;
  106.         else throw new Exception('Unauthorized Access to "__set_var()".');
  107.     }
  108.  
  109.     /**
  110.      * @param string $var name of attribute
  111.      * @return mixed
  112.      */
  113.     public function __get($var) {
  114.         foreach(array_keys($this->imported_objects) as $key) {
  115.             if(array_key_exists($var, get_object_vars($this->imported_objects[$key]))) {
  116.                 return $this->imported_objects[$key]->{$var};
  117.             }
  118.         }
  119.     }
  120.  
  121.     /**
  122.      * @param string $var name of attribute
  123.      * @param mixed $value
  124.      */
  125.     public function __set($var, $value) {
  126.         foreach(array_keys($this->imported_objects) as $key) {
  127.             if(array_key_exists($var, get_object_vars($this->imported_objects[$key])))
  128.                 $this->imported_objects[$key]->{$var} = $value;
  129.         }
  130.     }
  131. }
  132.  
  133. // Demo classes ----------------------------------------------
  134.  
  135. class User extends MI_Base
  136. {
  137.     protected $first_name;
  138.     protected $last_name;
  139.  
  140.     public function __construct($first, $last) {
  141.         $this->import(new UserFunctions);
  142.         $this->import(new MoreUserFunctions);
  143.         $this->first_name = $first;
  144.         $this->last_name = $last;
  145.     }
  146.  
  147.     public function getFullName() {
  148.         return $this->first_name . ' ' . $this->last_name;
  149.     }
  150. }
  151.  
  152. class UserFunctions extends MI_Importable
  153. {
  154.     public function lastThenFirst() {
  155.         return $this->last_name . ', ' . $this->first_name;
  156.     }
  157.  
  158.     public function appendToFirstname($name) {
  159.         $this->first_name .= $name;
  160.     }
  161.  
  162.     public function doExtentedFunction($statement) {
  163.         return 'Im very dynamic ' . $statement . ' added by ' . $this->first_name;
  164.     }
  165. }
  166.  
  167. class MoreUserFunctions extends MI_Importable
  168. {
  169.     public $position = 'none';
  170.  
  171.     public function sayHello() {
  172.         return 'Hello, my name is '.$this->getFullName().'.';
  173.     }
  174.    
  175.     public function getName() {
  176.         return 'Name: '.$this->lastThenFirst();
  177.     }
  178.    
  179.     public function getPosition() {
  180.         return 'Position: '.$this->position;
  181.     }
  182. }
  183.  
  184. // Demo output ----------------------------------------------
  185.  
  186. $user = new User('John', 'Doe');
  187.  
  188. echo $user->getFullName() . '<br />';
  189. $user->appendToFirstname('ny');
  190. echo $user->lastThenFirst() . '<br />';
  191. echo $user->doExtentedFunction('bullshit'). '<br />';
  192. echo $user->sayHello() . '<br />';
  193. echo $user->getName() . '<br />';
  194.  
  195. echo $user->position . '<br />';
  196. $user->position = 'Administrator';
  197. echo $user->getPosition() . '<br />';
  198.  
  199. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement