4

is there any way to simplify this code to avoid the need for an if to skip to the switch's default value?

I have a configuration table for different authentication methods for a http request, with an option not to set the value to default to a plain http request:

if(!isset($type)) {
    $type = "default";
}

switch ($type) {
   case "oauth":
       #instantinate an oauth class here
       break;
   case "http":
       #instantinate http auth class here
       break;
   default:
       #do an unprotected http request
       break;
}

I have no issue with the functionality, but I would like a cleaner solution to switch on an optional variable, is there any way to achieve that? Thanks!

3
  • Note that you do not need to set it to "default" - you could put that code in a function and use syntax function funcName($type = "") which would set it to "" if a null value was passed ib. Commented Sep 23, 2013 at 23:53
  • Interesting catch there @andrewb It's almost a little misleading in the code that the value if unset is set to the literal string 'default' and then execution in switch fall through to the 'default case'. default: is php syntax for an unmatched switch case. you may as well set $type to "" or "barbecuebits". Commented Apr 11, 2020 at 19:03
  • Yeah that was sort of the issue. I just had to set $type to something as if it wasn't set, I'd end up with an error. To this day I'm not sure I know a better solution.. Commented Apr 13, 2020 at 6:38

4 Answers 4

4

Just

switch ($type??'') {
    case "oauth":
        #instantinate an oauth class here
        break;
    case "http":
        #instantinate http auth class here
        break;
    default:
        #do an unprotected http request
        break;    
}

is enough on php >= 7

Sign up to request clarification or add additional context in comments.

Comments

4

You don't need to set the variable to "default". The default-case will be executed if the variable is not set or has any different value from all other defined cases. But remember: if the variable is not set and you use it in the switch, you will get a notice "Notice: Undefined variable". So if you don't want to disable notices you have to do the check if the variable is set.

4 Comments

I have failed to mention I need to avoid getting a Notice, so it seems there's no way to get around it without additional code, thanks.
While it may be frowned upon, you can suppress the Notice by placing @ in front of the variable.
@musicin3d This is not a good practice. You should not use the @ to supress errors etc. stackoverflow.com/questions/5573426/…
See, I understand it's bad practice to suppress errors, and it's usually bad to suppress anything. But this is a Notice, akin to a linter warning... about something we're already handling (with default:). And IMO switch(@ $var){...} is MUCH more readable than wrapping the whole block in if(isset($var)){...}. I mean, the "variable not set" Notice is the only use for the @ in this case, so you know exactly why it's there and it reduces indentation and clutter. This seems like one legitimate use case to me.
1

If you want to simplify it without getting a notice. Try the following:

if(!isset($type)) {
    #do an unprotected http request
}else{
    switch ($type) {
       case "oauth":
           #instantinate an oauth class here
           break;
       case "http":
           #instantinate http auth class here
           break;
    }
}

Comments

-1

The default case is a catch-all if none of the previous cases are found, so your check for whether the variable isset and assigning it to "default" is unnecessary.

Comments

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.