A PHP reflection library to directly access protected/private properties and call protected/private methods.
This library works with major versions of PHP 7.3.
composer require nsp-team/reflection-tool:~1.0.0
require __DIR__ . '/vendor/autoload.php';
use NspTeam\Reflection\ReflectionObject;
class Test
{
private $key;
private static $keyStatic;
/**
*
* @return string
*/
private function one(): string
{
return '私有方法';
}
/**
* @param int $i
* @param int $j
* @return string
*/
private static function oneStatic(int $i, int $j): string
{
return "私有静态方法 带参 $i 和 $j";
}
}
$test = new Test();
ReflectionObject::setProperty(Test::class, 'keyStatic', 'another value');
ReflectionObject::setProperty($test, 'key', 'value ');
var_dump(ReflectionObject::callMethod($test, 'one'));
var_dump(ReflectionObject::getProperty($test, 'key'));
var_dump(ReflectionObject::callMethod($test, 'oneStatic', array(1, 2)));
var_dump(ReflectionObject::getProperty($test, 'keyStatic'));
var_dump(ReflectionObject::findProperty($test, 'key'));
var_dump(ReflectionObject::getMethod(Test::class, 'one')->getDocComment());