I have a situation where I require to consult different objects in PHP via different methods to find some data.
The question regards more about formatting the code than an actual programming issue. What I am trying to do is not using several if‘s to gather this data like:
$data = obj->getData(); if (!isset($data)) $data = othObj->getThisData(); if (!isset($data)) $data = anothObj->getTheData(); if (!isset($data)) $data = anothOne->getAData(); ... process($data)
I was wondering what are the best practices in this case, if there is a better way using another procedures, like foreach or switch/case.
Thanks!
You can make an array of the possible objects you want to try, then run a loop. Might be more maintainable. This code can be modified to include parameters and use call_user_func_array instead.