首页 > Web开发 > 详细

php反射

时间:2019-12-03 12:08:55      阅读:65      评论:0      收藏:0      [点我收藏+]
//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反射

原文:https://www.cnblogs.com/xiajiaw/p/11974952.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!