Skip to content

Instantly share code, notes, and snippets.

@sunaot
Created July 26, 2011 08:13
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 sunaot/1106254 to your computer and use it in GitHub Desktop.
Save sunaot/1106254 to your computer and use it in GitHub Desktop.
<?php
// public ReflectionMethod::getClosure ( object $object ) alternative
function toClosure($class_name, $method_name, $obj = null) {
if (!is_null($obj) and !is_a($obj, $class_name)) throw new \InvalidArgumentException('invalid object is given.');
$func = new ReflectionMethod($class_name, $method_name);
if ($func->isStatic()) {
return function() use($func) {
$func->invokeArgs(null, func_get_args());
};
} else {
return function() use($func, $obj) {
$func->invokeArgs($obj, func_get_args());
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment