Skip to content

Instantly share code, notes, and snippets.

@lisachenko
Created March 6, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lisachenko/f1e011685289c77b4324 to your computer and use it in GitHub Desktop.
Save lisachenko/f1e011685289c77b4324 to your computer and use it in GitHub Desktop.
Method accessor as closures
class MagicMethodAccessor {
public function test()
{
echo 'Cool';
}
final public function __get($name)
{
if (method_exists($this, $name)) {
$method = (new ReflectionMethod($this, $name))->getClosure($this);
return $method;
}
return null;
}
final public function __call($name, $arguments)
{
$method = $this->$name;
$rebind = Closure::bind($method, $this, get_called_class());
return call_user_func_array($rebind, $arguments);
}
}
$obj = new MagicMethodAccessor();
$method = $obj->test; // Will return closure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment