Created
December 1, 2011 16:45
-
-
Save andreasjansson/1418108 to your computer and use it in GitHub Desktop.
Call private and private static functions
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 | |
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