Skip to content

Instantly share code, notes, and snippets.

@mustafaileri
Last active January 27, 2017 14:02
Show Gist options
  • Save mustafaileri/f0d92e08629d72ce35d2643a1507a074 to your computer and use it in GitHub Desktop.
Save mustafaileri/f0d92e08629d72ce35d2643a1507a074 to your computer and use it in GitHub Desktop.
Call a private or protected method or property.
<?php
private function invokeRestrictedMethodAndProperties($object, $methodName, $args = [], $properties = [])
{
$reflectionClass = new \ReflectionClass(get_class($object));
$method = $reflectionClass->getMethod($methodName);
$method->setAccessible(true);
foreach ($properties as $propertyKey => $value) {
$prop = $reflectionClass->getProperty($propertyKey);
$prop->setAccessible(true);
$prop->setValue($object, $value);
}
return $method->invokeArgs($object, $args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment