Created
January 16, 2011 01:25
-
-
Save rhiguchi/781445 to your computer and use it in GitHub Desktop.
A function for invoking private or protected method with reflection.
This file contains 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
/** | |
* 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