0

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;
4
  • What has $x and $y got to do with the price of cheese? Commented Aug 27, 2015 at 19:40
  • And what is your problem/ question now? Just do: $$cls->$prp Commented Aug 27, 2015 at 19:40
  • Why should test 3 work? Commented Aug 27, 2015 at 19:43
  • Rizier123, that worked great. Thanks!! Commented Aug 27, 2015 at 19:48

1 Answer 1

0

Use this:

echo 'test3 = ' . ${$cls}->{$prp} . '<br>';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. I just couldn't get the right syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.