Skip to content

Instantly share code, notes, and snippets.

@andreasjansson
Created December 1, 2011 16:44
Show Gist options
  • Save andreasjansson/1418093 to your computer and use it in GitHub Desktop.
Save andreasjansson/1418093 to your computer and use it in GitHub Desktop.
PHP callPrivate, callPrivateStatic
function callPrivate($object, $methodName /*, $arg1, $arg2, ... */)
{
if(!is_object($object))
throw new Exception('$object is not an object');
if(!method_exists($object, $methodName))
throw new Exception("\$object has no method $method");
$args = array_slice(func_get_args(), 2);
$method = new ReflectionMethod(get_class($object), $methodName);
$method->setAccessible(TRUE);
return $method->invokeArgs($object, $args);
}
function callPrivateStatic($className, $methodName /*, $arg1, ... */)
{
if(!class_exists($className))
throw new Exception("Class $className does not exist");
if(!method_exists($className, $methodName))
throw new Exception("$className has no static method $method");
$args = array_slice(func_get_args(), 2);
$method = new ReflectionMethod($className, $methodName);
$method->setAccessible(TRUE);
return $method->invokeArgs(NULL, $args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment