Created
January 10, 2018 18:10
-
-
Save imliam/f430f289a87aa1304d754972271c7fb4 to your computer and use it in GitHub Desktop.
Trait to dynamically bind methods to a class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait BindMethods | |
{ | |
private $boundMethods = []; | |
public function bindMethod($methodName, $method) { | |
$this->boundMethods[$methodName] = Closure::bind($method, $this, get_class()); | |
} | |
function __call($method, $args) { | |
if (is_callable('parent::__call')) { | |
parent::__call($method, $args); | |
} | |
if(is_callable($this->boundMethods[$method])) | |
{ | |
return call_user_func_array($this->boundMethods[$method], $args); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: