本文介紹了我可以通過 Reflection 獲取私有財產的價值嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
它似乎不起作用:
$ref = new ReflectionObject($obj);
if($ref->hasProperty('privateProperty')){
print_r($ref->getProperty('privateProperty'));
}
它進入IF循環,然后拋出一個錯誤:
It gets into the IF loop, and then throws an error:
屬性 privateProperty 不存在
Property privateProperty does not exist
:|
$ref = new ReflectionProperty($obj, 'privateProperty')
也不起作用...
文檔頁面列出了一些常量,包括 IS_PRIVATE
.如果我無法訪問私有財產,我怎么能使用它呢?
The documentation page lists a few constants, including IS_PRIVATE
. How can I ever use that if I can't access a private property lol?
推薦答案
class A
{
private $b = 'c';
}
$obj = new A();
$r = new ReflectionObject($obj);
$p = $r->getProperty('b');
$p->setAccessible(true); // <--- you set the property to public before you read the value
var_dump($p->getValue($obj));
這篇關于我可以通過 Reflection 獲取私有財產的價值嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!