classtest{private$a=array();publicfunction__construct(){$this->geta();}privatefunctiongeta(){$b[]=1;$b[]=2;$this->a=$b;//没有return}}//反射获取$ref_class=newReflectionClass('test');$geta=$ref_class->newInstance();$method=$ref_class->getmethod('geta');$method->setAccessible(true);$a=$method->invoke($rongyuclass);//空的,因为geta不返回任何值有这样的一个类,现在里面的私有方法geta没有return出任何数据,但是构造方法那边给私有属性a赋值,我直接用反射去取变量$a的时候得到的只能是空值,如何先执行构造,然后得到赋值后的私有属性a?
2 回答
![?](http://img1.sycdn.imooc.com/5458463b0001358f02200220-100-100.jpg)
ibeautiful
TA贡献1993条经验 获得超5个赞
$ref=newReflectionClass('test');$a=$ref->getProperty('a');$a->setAccessible(true);//设置属性a的访问权限var_dump($a->getValue(newtest));
![?](http://img1.sycdn.imooc.com/54584d080001566902200220-100-100.jpg)
潇潇雨雨
TA贡献1833条经验 获得超4个赞
classtest{private$a=array();publicfunction__construct(){$this->geta();}privatefunctiongeta(){$b[]=1;$b[]=2;$this->a=$b;//没有return}}$test=newtest;$closure=function(){return$this->a;};//rebindstheclosuretothe$testobject$closure=$closure->bindTo($test,$test);print_r($closure());
添加回答
举报
0/150
提交
取消