Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @package Multiple Inheritance
- * @author Martin Pietschmann <schirkan86@gmx.de>
- */
- /**
- * Inheritanced classes will have full access to
- * the methods and attributes (public and protected)
- * of the Base class they are registered with.
- */
- class MI_Importable
- {
- private $base = null;
- /**
- * Registers this Object with the Base class
- * @param Base $base
- */
- final public function register(MI_Base $base) {
- $this->base = $base;
- }
- /**
- * Calls a method of $this->base
- * @param string $method
- * @param string $args
- * @return mixed
- */
- final public function __call($method, $args) {
- return call_user_func_array(array($this->base, $method), $args);
- }
- /**
- * @param string $var name of attribute
- * @return mixed
- */
- final public function __get($var) {
- return $this->base->__get_var($this, $var);
- }
- /**
- * @param string $var name of attribute
- * @param mixed $value
- */
- final public function __set($var, $value) {
- return $this->base->__set_var($this, $var, $value);
- }
- }
- class MI_Base
- {
- private $imported_objects = array();
- private $imported_functions = array();
- /**
- * Import method
- * @param MI_Importable $new_import
- */
- final protected function import(MI_Importable $new_import) {
- $new_import->register($this);
- $this->imported_objects[] = &$new_import;
- // the new functions to import
- $import_functions = get_class_methods(get_class($new_import));
- $MI_Importable_functions = get_class_methods('MI_Importable');
- foreach($import_functions as $function_name) {
- if(in_array($function_name, $MI_Importable_functions)) continue;
- if(isset($this->imported_functions[$function_name]))
- throw new Exception('Duplicated function name: '.$function_name);
- $this->imported_functions[$function_name] = &$new_import;
- }
- }
- /**
- * Calls a method
- * @param string $method
- * @param string $args
- * @return mixed
- */
- final public function __call($method, $args) {
- // make sure the function exists
- if(isset($this->imported_functions[$method]))
- return call_user_func_array(array($this->imported_functions[$method], $method), $args);
- throw new Exception ('Call to undefined method/class function: ' . $method);
- }
- /**
- * Gets a public or protected attribute
- * @param MI_Importable $caller
- * @param string $var name of attribute
- * @return mixed
- */
- final public function __get_var(MI_Importable $caller, $var) {
- if(in_array($caller, $this->imported_objects)) return $this->{$var};
- else throw new Exception('Unauthorized Access to "__get_var()".');
- }
- /**
- * Sets a public or protected attribute
- * @param MI_Importable $caller
- * @param string $var name of attribute
- * @param mixed $value
- */
- final public function __set_var(MI_Importable $caller, $var, $value) {
- if(in_array($caller, $this->imported_objects)) $this->{$var} = $value;
- else throw new Exception('Unauthorized Access to "__set_var()".');
- }
- /**
- * @param string $var name of attribute
- * @return mixed
- */
- public function __get($var) {
- foreach(array_keys($this->imported_objects) as $key) {
- if(array_key_exists($var, get_object_vars($this->imported_objects[$key]))) {
- return $this->imported_objects[$key]->{$var};
- }
- }
- }
- /**
- * @param string $var name of attribute
- * @param mixed $value
- */
- public function __set($var, $value) {
- foreach(array_keys($this->imported_objects) as $key) {
- if(array_key_exists($var, get_object_vars($this->imported_objects[$key])))
- $this->imported_objects[$key]->{$var} = $value;
- }
- }
- }
- // Demo classes ----------------------------------------------
- class User extends MI_Base
- {
- protected $first_name;
- protected $last_name;
- public function __construct($first, $last) {
- $this->import(new UserFunctions);
- $this->import(new MoreUserFunctions);
- $this->first_name = $first;
- $this->last_name = $last;
- }
- public function getFullName() {
- return $this->first_name . ' ' . $this->last_name;
- }
- }
- class UserFunctions extends MI_Importable
- {
- public function lastThenFirst() {
- return $this->last_name . ', ' . $this->first_name;
- }
- public function appendToFirstname($name) {
- $this->first_name .= $name;
- }
- public function doExtentedFunction($statement) {
- return 'Im very dynamic ' . $statement . ' added by ' . $this->first_name;
- }
- }
- class MoreUserFunctions extends MI_Importable
- {
- public $position = 'none';
- public function sayHello() {
- return 'Hello, my name is '.$this->getFullName().'.';
- }
- public function getName() {
- return 'Name: '.$this->lastThenFirst();
- }
- public function getPosition() {
- return 'Position: '.$this->position;
- }
- }
- // Demo output ----------------------------------------------
- $user = new User('John', 'Doe');
- echo $user->getFullName() . '<br />';
- $user->appendToFirstname('ny');
- echo $user->lastThenFirst() . '<br />';
- echo $user->doExtentedFunction('bullshit'). '<br />';
- echo $user->sayHello() . '<br />';
- echo $user->getName() . '<br />';
- echo $user->position . '<br />';
- $user->position = 'Administrator';
- echo $user->getPosition() . '<br />';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement