I have an object in PHP (in my case, a CakePHP model), that has child objects, eg. the structure of $this->Model1->Model2
.
I am aware that the above syntax is the correct way to reference the object, but if I have obtained the name of Model2
via another process, so it is a string variable. How can I reference it using this variable, similar to as follows:
$model_name = 'Model2';
$this->Model1[ $model_name ]->run_function();
Now clearly this array-style of selecting the object/class doesn't work, but is there a similar method I should be using? Or is there better coding practice that wouldn't result in this issue all together?