Skip to content

Instantly share code, notes, and snippets.

@rhiguchi
Created January 16, 2011 01:25
Show Gist options
  • Save rhiguchi/781445 to your computer and use it in GitHub Desktop.
Save rhiguchi/781445 to your computer and use it in GitHub Desktop.
A function for invoking private or protected method with reflection.
/**
* Invoke method by using reflection.
* Require PHP5 (>= 5.3.2)
* @param $method_name
* @param $obj - An object that invokes the method.
* @param mothod_args... Arguments for the method.
*/
public static function invoke_method($method_name, $obj) {
$class_name = get_class($obj);
$class = new ReflectionClass($class_name);
$method = $class->getMethod($method_name);
$method->setAccessible(true);
$mothod_args = func_get_args();
array_shift($mothod_args);
array_shift($mothod_args);
return $method->invokeArgs($obj, $mothod_args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment