Skip to content

Instantly share code, notes, and snippets.

@haruta
Created August 12, 2012 15:59
Show Gist options
  • Save haruta/3332452 to your computer and use it in GitHub Desktop.
Save haruta/3332452 to your computer and use it in GitHub Desktop.
private method access for php using Reflection
<?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