Skip to content

Instantly share code, notes, and snippets.

@djekl
Created March 31, 2020 18:04
Show Gist options
  • Save djekl/d6bd0397c3d360cfe0dc39be2c204c07 to your computer and use it in GitHub Desktop.
Save djekl/d6bd0397c3d360cfe0dc39be2c204c07 to your computer and use it in GitHub Desktop.
<?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