php 变量变量访问对象的 属性

php variable variables to access property of an object

我想在 table 中存储一个 属性-> 对象名称并在代码中访问。

完成以下任务的正确方法是什么。

$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>';