//MyClass这个类中包含了一个名为myFun的私有方法
class MyClass {
private $tmp = ‘hello‘;
private function myFun()
{
echo $this->tmp . ‘ ‘ . ‘world!‘;
}
}
//通过类名MyClass进行反射
$ref_class = new ReflectionClass(‘MyClass‘);
//通过反射类进行实例化
$instance = $ref_class->newInstance();
//通过方法名myFun获取指定方法
$method = $ref_class->getMethod(‘myFun‘);
//设置可访问性
$method->setAccessible(true);
//执行方法
$method->invoke($instance);
//获取属性
$property = $ref_class->getProperty(‘tmp‘);
//打印属性
var_dump($property);
原文:https://www.cnblogs.com/xiajiaw/p/11974952.html