Created
March 31, 2020 18:04
-
-
Save djekl/d6bd0397c3d360cfe0dc39be2c204c07 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Call protected/private method of a class. | |
* | |
* @param object &$object Instantiated object that we will run method on. | |
* @param string $methodName Method name to call | |
* @param array $parameters Array of parameters to pass into method. | |
* | |
* @return mixed Method return. | |
*/ | |
public function invokeMethod(&$object, $methodName, array $parameters = []) | |
{ | |
$reflection = new \ReflectionClass(get_class($object)); | |
$method = $reflection->getMethod($methodName); | |
$method->setAccessible(true); | |
return $method->invokeArgs($object, $parameters); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment