[ACCEPTED]-switch statement without break-switch-statement

Accepted answer
Score: 18

Fallthrough was an intentional design feature 9 for allowing code like:

switch ($command) {
  case "exit":
  case "quit":
    quit();
    break;
  case "reset":
    stop();
  case "start":
    start();
    break;
}

It's designed so 8 that execution runs down from case to case.

default is 7 a case like any other, except that jumping 6 there happens if no other case was triggered. It 5 is not by any means a "do this after running 4 the actual selected case" instruction. In 3 your example, you could consider:

  switch($param) {
    case "created":
        if(!($value instanceof \DateTime))
            throw new \Exception("\DateTime expected, ".gettype($value)." given for self::$param");
        break;
    case "Creator":
        if(!($value instanceof \Base\User)) {
            throw new \Exception(get_class($value)." given. \Base\User expected for self::\$Creator");                  
        }
        break;
}

$this->$param = $value;

The rule 2 of thumb here is, if it doesn't depend on 1 the switch, move it out of the switch.

Score: 1

Because that's how it's done in C.

0

Score: 1

Perhaps this will enlighten you:

Jump Table Switch Case question

0

Score: 1

In PHP 8 we have match, similar with switch expression 2 but is significantly shorter:

  • it doesn't require a break statement
  • it can combine different arms into one using a comma
  • it returns a value, so you only have to assign value once

An example:

$message = match ($statusCode) {
    200, 300 => null,
    400 => 'not found',
    500 => 'server error',
    default => 'unknown status code',
};

Here's 1 its switch equivalent:

switch ($statusCode) {
    case 200:
    case 300:
        $message = null;
        break;
    case 400:
        $message = 'not found';
        break;
    case 500:
        $message = 'server error';
        break;
    default:
        $message = 'unknown status code';
        break;
}

reference : https://stitcher.io/blog/php-8-match-or-switch

Score: 0

To answer your "actual question": Why does it continue with the "Creator" case while the case is not "Creator".

Because 6 you do not have break. Without it, it will continue 5 to the cases below it. The only solution 4 I can think of is putting your default code 3 to the cases, and add break.

Also, you don't need 2 break on the default case since its the last 1 case in the switch block.

Score: 0

I don't really see what you want.

  1. If you want to run the default stuff in all cases, just put it after the switch.
  2. If you want to run the default stuff only in the "created" case and in the default case, swap the position of the "created" and "Creator" sections and put a break after the first.
  3. If you want that code to only run if Creator or created matches, then get rid of the switch statement and use an if/else OR use a flag and a following if statement.

All the 1 tools are there.

More Related questions