Created
August 12, 2012 15:59
-
-
Save haruta/3332452 to your computer and use it in GitHub Desktop.
private method access for php using 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
<?php | |
class Ex1 { | |
private function private_method($val1 , $val2) { | |
return func_get_args(); | |
} | |
} | |
$obj = new Ex1(); | |
$r = call_private_method($obj, 'private_method', 'test1', 'test2'); | |
var_dump($r); | |
function call_private_method($obj, $method_name) | |
{ | |
$args = array_slice(func_get_args(), 2); | |
array_unshift($args, $obj); | |
$ref_obj = new ReflectionObject($obj); | |
$ref_method = $ref_obj->getMethod($method_name); | |
$ref_method->setAccessible(true); | |
return call_user_func_array(array($ref_method, 'invoke'), $args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment