Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save you-think-you-are-special/96596dac703a521da361 to your computer and use it in GitHub Desktop.
Save you-think-you-are-special/96596dac703a521da361 to your computer and use it in GitHub Desktop.
Call private method in php for testing
<?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