第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

php反射類如何獲取構(gòu)造方法得到的私有屬性

php反射類如何獲取構(gòu)造方法得到的私有屬性

PHP
慕工程0101907 2019-03-18 15:24:13
class test{ private $a=array(); public function __construct() { $this->geta (); } private function geta(){ $b [] = 1; $b [] = 2; $this->a = $b; // 沒(méi)有return } } //反射獲取 $ref_class = new ReflectionClass('test'); $geta= $ref_class->newInstance(); $method = $ref_class->getmethod('geta'); $method->setAccessible(true); $a = $method->invoke($rongyuclass); //空的,因?yàn)間eta不返回任何值 有這樣的一個(gè)類,現(xiàn)在里面的私有方法geta沒(méi)有return出任何數(shù)據(jù),但是構(gòu)造方法那邊給私有屬性a賦值,我直接用反射去取變量$a的時(shí)候得到的只能是空值,如何先執(zhí)行構(gòu)造,然后得到賦值后的私有屬性a?
查看完整描述

2 回答

?
猛跑小豬

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊

$ref = new ReflectionClass('test');
$a = $ref->getProperty('a');
$a->setAccessible(true); //設(shè)置屬性a的訪問(wèn)權(quán)限
var_dump($a->getValue(new test));

查看完整回答
反對(duì) 回復(fù) 2019-03-18
?
炎炎設(shè)計(jì)

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊

class test{
    private $a=array();
    public function __construct() {
        $this->geta ();
    }
    private function geta(){
        $b [] = 1;
        $b [] = 2;
        
        $this->a = $b;
        // 沒(méi)有return
    }
}

$test = new test;

$closure = function() {
    return $this->a;
};
// rebinds the closure to the $test object
$closure  = $closure->bindTo($test, $test);
print_r($closure());
查看完整回答
反對(duì) 回復(fù) 2019-03-18
  • 2 回答
  • 0 關(guān)注
  • 658 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)