Advertisement
Guest User

crem0r

a guest
Jan 8th, 2009
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. <?php
  2.  
  3. class Base
  4. {
  5.     private $imported_functions = array();
  6.  
  7.     public function import($new_import)
  8.     {
  9.         $new_import->register($this);
  10.  
  11.         // the new functions to import
  12.         $import_functions   = get_class_methods(get_class($new_import));
  13.  
  14.         foreach($import_functions as $key => $function_name)
  15.         {
  16.             $this->imported_functions[$function_name] = &$new_import;
  17.         }
  18.     }
  19.  
  20.     public function __call($method, $args)
  21.     {
  22.         // make sure the function exists
  23.         if(array_key_exists($method, $this->imported_functions))
  24.         {
  25.             return call_user_func_array(array($this->imported_functions[$method], $method), $args);
  26.         }
  27.         throw new Exception ('Call to undefined method/class function: ' . $method);
  28.     }
  29. }
  30.  
  31. class User extends Base
  32. {
  33.     public $first_name;
  34.     public $last_name;
  35.  
  36.     public function __construct()
  37.     {
  38.         $this->first_name = 'Ian';
  39.         $this->last_name = 'Selby';
  40.     }
  41.  
  42.     public function getFullName()
  43.     {
  44.         return $this->first_name . ' ' . $this->last_name;
  45.     }
  46. }
  47.  
  48. class UserFunctions
  49. {
  50.     protected $that = null;
  51.  
  52.     public function register($that)
  53.     {
  54.         $this->that = $that;
  55.     }
  56.  
  57.     public function lastThenFirst()
  58.     {
  59.         return $this->that->last_name . ', ' . $this->that->first_name;
  60.     }
  61.  
  62.     public function appendToLastname($name)
  63.     {
  64.         $this->that->last_name .= $name;
  65.     }
  66.  
  67.     public function doExtentedFunction($statement)
  68.     {
  69.         return 'Im very dynamic ' . $statement . ' added by ' . $this->that->last_name;
  70.     }
  71. }
  72.  
  73. $user = new User();
  74. $user->import(new UserFunctions());
  75.  
  76. echo $user->getFullName() . '<br />';
  77. echo $user->appendToLastname('peterson');
  78. echo $user->lastThenFirst() . '<br />';
  79. echo $user->doExtentedFunction('bullshit');
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement