- //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);
PHP 反射
来源: http://www.bubuko.com/infodetail-3314752.html