views:

102

answers:

5

Heyall,

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!

A: 
  1. It doesn't look too bad the way it is.
  2. It could be a bit more efficient if the if's were nested. e.g.

     if (!isset($data = othObj->getData()))
     if (!isset($data = othObj->getThisData()))
     if (!isset($data = anothObj->getTheData()))
     $data = anothOne->getAData()))
     // ...
     process($data)
    

    Since there are less calls to isset (though they're pretty cheap anyway, so I wouldn't worry about it).

Assaf Lavie
A: 

Personally I'd do something like this:

$data = null;

if (isset($obj->getData()) $data = $obj->getData();
else if (isset($othObj->getThisData()) $data = $othObj->getThisData();
else if (isset($anothObj->getTheData()) $data = $anothObj->getTheData();
else if (isset($anothOne->getAData()) $data = $anothOne->getAData();

process($data)

This saves processing time if the earlier objects actually return something. Since it is an elseif setup once it finds data it'll stop processing the other if clauses.

I don't think a switch statement would be appropriate in this case. Switches tend to be testing the value of one variable (is $a = 1, 2, 3 or 4).

Parrots
+2  A: 

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.

$dataCallback = array(
    array($othObj, 'getData'),
    array($othObj, 'getThisData'),
    array($anothObj, 'getTheData'),
    array($anothOne, 'getAData'),
);

for($i = 0, $t = count($dataCallback); !isset($data) && $i < $t; $i++) {
  $callback = $dataCallback[$i];
  $data = call_user_func($callback);
}

if (isset($data))
  process($data);
else
  //no valid data returned at all ...
OIS
sorry but this is just a mess. i would rather use the code he propose, why to complicate things?
dusoft
Id stick with my version and make it a function. $dataCallback[] = array($oneMore, 'method'); $data = get_data($dataCallback); better then maintaining lines of if if if if if if if ... gets confusing if you got enough.
OIS
A: 
($data = $ob1->get()) || ($data = $ob2->get()) || ($data = $ob3->get());

Would work, but if you're get function returns an empty array or false or empty string instead of NULL, it's going to continue looking for data...

Mario
you have to rewrite it: ($data = $ob1->get()) || ($data = $ob2->get()) || ($data = $ob3->get());
OIS
Oops! You're right.
Mario
A: 

I'd probably group the objects to ask for data into an array:

$objArray = array($obj, $othObj, $anothObj, ... );

Then run through a while loop until I had data:

$i = 0;
do {
   $data = $objArray[$i]->getData();
   $i++;
} while(!isset($data) && $i < count($objArray));
acrosman