Created
June 9, 2015 11:32
-
-
Save you-think-you-are-special/96596dac703a521da361 to your computer and use it in GitHub Desktop.
Call private method in php for testing
This file contains hidden or 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 callPrivateMethod($object, $method, $args) | |
{ | |
$classReflection = new \ReflectionClass(get_class($object)); | |
$methodReflection = $classReflection->getMethod($method); | |
$methodReflection->setAccessible(true); | |
$result = $methodReflection->invokeArgs($object, $args); | |
$methodReflection->setAccessible(false); | |
return $result; | |
} | |
$myObject = new MyClass(); | |
callPrivateMethod($myObject, 'hello', ['world']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment