php反射

//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);

  

posted @ 2019-12-03 09:50  夏嘉  阅读(117)  评论(0编辑  收藏  举报