Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- trait TEnhance {
- //Array to hold new method Closures
- protected static $added_methods = [];
- //Adds a new method to the class
- public static function add_method($name, $closure) {
- static::$added_methods[get_called_class()][$name] = $closure;
- }
- //Looks through the inheritance hierachy for an added method with
- //the given name, returning the Closure if found, throwing if not
- protected static function find_method($name) {
- $class = get_called_class();
- if(
- array_key_exists($class, static::$added_methods) &&
- array_key_exists($name, static::$added_methods[$class])
- ) return static::$added_methods[$class][$name];
- $parent = get_parent_class($class);
- if(!$parent) throw new Exception('No such method =(');
- return $parent::find_method($name);
- }
- //
- public function __call($name, $args) {
- return call_user_func_array(
- static::find_method($name)->bindTo($this, $this),
- $args
- );
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement