Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Base
- {
- private $imported_functions = array();
- public function import($new_import)
- {
- $new_import->register($this);
- // the new functions to import
- $import_functions = get_class_methods(get_class($new_import));
- foreach($import_functions as $key => $function_name)
- {
- $this->imported_functions[$function_name] = &$new_import;
- }
- }
- public function __call($method, $args)
- {
- // make sure the function exists
- if(array_key_exists($method, $this->imported_functions))
- {
- return call_user_func_array(array($this->imported_functions[$method], $method), $args);
- }
- throw new Exception ('Call to undefined method/class function: ' . $method);
- }
- }
- class User extends Base
- {
- public $first_name;
- public $last_name;
- public function __construct()
- {
- $this->first_name = 'Ian';
- $this->last_name = 'Selby';
- }
- public function getFullName()
- {
- return $this->first_name . ' ' . $this->last_name;
- }
- }
- class UserFunctions
- {
- protected $that = null;
- public function register($that)
- {
- $this->that = $that;
- }
- public function lastThenFirst()
- {
- return $this->that->last_name . ', ' . $this->that->first_name;
- }
- public function appendToLastname($name)
- {
- $this->that->last_name .= $name;
- }
- public function doExtentedFunction($statement)
- {
- return 'Im very dynamic ' . $statement . ' added by ' . $this->that->last_name;
- }
- }
- $user = new User();
- $user->import(new UserFunctions());
- echo $user->getFullName() . '<br />';
- echo $user->appendToLastname('peterson');
- echo $user->lastThenFirst() . '<br />';
- echo $user->doExtentedFunction('bullshit');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement