簡體   English   中英

php變量變量來訪問對象的屬性

[英]php variable variables to access property of an object

我想在表中存儲一個property-> object name並在代碼中訪問。

什么是完成以下任務的正確方法。

$patient = new stdClass();
$patient->patient_number = '12345';

$cls = 'patient';
$prp = 'patient_number';



echo 'test1 = ' . $patient->patient_number . '<br>';    
//--- this prints 12345 as expected;
echo 'test2 = ' . $$cls->patient_number . '<br>';
//--- this print 12345 as expected;
echo 'test3 = ' . $$cls->${$prp} . '<br>';
//--- this generates undefined variable patient_number;

用這個:

echo 'test3 = ' . ${$cls}->{$prp} . '<br>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM