简体   繁体   English

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

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

I want to store an property->object name in a table and access in code. 我想在表中存储一个property-> object name并在代码中访问。

What is the proper way to accomplish the following. 什么是完成以下任务的正确方法。

$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