views:

113

answers:

6

I would like to know if there is anyway I can do this without having to do the same If loop in each of the Switch cases since it's a bit repetitive.

switch ($val) {
    case 1:
     if(!$object->doSomething1()==$goodValue)
     row .= $object->doSomething();
     break;
    case 2:
     if(!$object->doSomething2()==$goodValue)
     row .= $object->doSomething();
     break;
    case 3:
     if(!$object->doSomething3()==$goodValue)
     row .= $object->doSomething();
     break;
}
+4  A: 
switch ($val) {
    case 1:
    case 2:
    case 3:
        if(!$object->doSomething{$val}()==$goodValue)
        row .= $object->doSomething();
        break;
}
meanstreakuk
I think that you missed that he was calling a different function for each case. Otherwise this would be a great idea.
Tom
Note the curly braced $val after doSomething in the if statement in my example - if his example 'as is' is what he requires, my answer would work.
meanstreakuk
at that point, why even have the switch statement at all?
GSto
Ok, and for sending me back to the manual to check your code +1
Tom
@GSto - indeed! +1
meanstreakuk
I suppoer if $val == 0, we don't want to call doSomething0()...
meanstreakuk
+1  A: 

Why don't you just wrap the entire case in the if loop?

if(!$object->doSomething()==$goodValue) {
    switch ($val) {
        case 1:
            row .= $object->doSomething();
            break;
        case 2:
            row .= $object->doSomething();
            break;
        case 3:
            row .= $object->doSomething();
            break;
    }
}
ryeguy
sorry I forgot to mention that the function called is not the same in each case.
mnml
You *did* notice there's actually a different function name in the if statement for each value, right?
paxdiablo
if the function called in each case is not the same then it s not repetitive. is it?
Kennethvr
is $goodvalue the same in each case?
ryeguy
@pax, @kennethvr - before his edit he was calling the same function.
ryeguy
+1  A: 

Without knowing much about what doSomething1/2/3 do it sounds like you need too much information on the internals of $object. It might be worth exposing a function on $object that can take $val instead and doSomething1/2/3 depending on $val internally.

if(!$object->doSomething($val)==$goodValue)
        row .= $object->doSomething();
        break;
}
Martijn Laarman
+1  A: 
switch ($val) {
    case 1:
        $method .= 'method1';
        break;
    case 2:
        $method .= 'method2';
        break;
    case 3:
        $method .= 'method3';
        break;
}
$funcVal = $object->$method();
if(!$funcVal==$goodValue) $row .= $funcVal;
Nicky De Maeyer
if(!$object->$method()==$goodValue) $row .= $object->$method();I'm trying this
mnml
ok, but the point remains the same, the action performed is the same, only the method calls differ, so only those should be switched...
Nicky De Maeyer
But if you are going to perform the same method call 2 times, it's better to store the result...
Nicky De Maeyer
yeah it worked fine
mnml
In the examples above can you eliminate the switch altogether by composing $method from the function name and $val?
Jackson
+1  A: 

if isn’t a loop, it’s a condition, you might be confusing some.

as to your question: since you are testing different methods there is no (simple, sane) way of not repeating the if’s. you aren’t after all, they all test different conditions.

knittl
+1  A: 

without assuming continuous method names, or using stuff like call_user_func(), just a slight modification:

switch ($val) {
    case 1:
        $doResult = $object->doSomething1();
        break;
    case 2:
        $doResult = $object->doSomething2();
        break;
    case 3:
        $doResult = $object->doSomething3();
        break;
}
if($doResult !== $goodValue)
    row .= $object->doSomething();
jcinacio