简体   繁体   中英

php variable variables to access property of an object

I want to store an property->object name in a table and access in code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM