提问人:Ken Roberts 提问时间:8/28/2015 最后编辑:Ken Roberts 更新时间:8/28/2015 访问量:43
用于访问对象属性的 PHP 变量变量
php variable variables to access property of an object
问:
我想将属性>对象名称存储在表中,并在代码中访问。
完成以下操作的正确方法是什么。
$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;
答:
0赞
Alvaro Gonzalez
8/28/2015
#1
使用这个:
echo 'test3 = ' . ${$cls}->{$prp} . '<br>';
评论
0赞
Ken Roberts
8/28/2015
非常感谢。我只是无法获得正确的语法。
上一个:如何到达有空格的变量?
下一个:使用变量填充数组会导致错误
评论
$$cls->$prp