Switch statement without breaking

Why does the case argument in a switch statement that does not contain a break automatically jump to the next case without checking?

try { switch($param) { case "created": if(!($value instanceof \DateTime)) throw new \Exception("\DateTime expected, ".gettype($value)." given for self::$param"); case "Creator": if(!($value instanceof \Base\User)) { throw new \Exception(get_class($value)." given. \Base\User expected for self::\$Creator"); } default: $this->$param = $value; break; } } catch(Exception $e) { echo $e->getMessage(); } 

If the parameter is "created", it will check in the created case, which is good. When the verification is successful, I want the code to continue the default installation, so there is no break ;. But instead, he continues to "Creator", and $ param! = "Creator"!

I know how to solve this (just add the default code in my β€œcreated” case), but I don't like to repeat this code too often. My actual question is: why does it continue with the "Creator", while the matter is not the "Creator".

+5
php switch-statement
Nov 11 2018-10-11
source share
5 answers

The patch was an intentional design feature that allowed the use of this code:

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

It is designed in such a way that execution is performed from case to case.

default is a case like any other, except that jumps occur if no other case has been called. This in no way means "do it after following the instructions for a particular case." In your example, you can 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 of thumb is here, if it is independent of the switch, pull it out of the switch.

+16
Nov 11 '10 at 14:50
source share

Since it did in C.

+1
Nov 11 2018-10-11
source share

Perhaps this will enlighten you:

Question about transition Table Table

+1
Nov 11 2018-10-11
source share

To answer your "factual question": why does he continue to work with the "Creator", while the matter is not the "Creator".

Because you have no break . Without this, he will continue to review cases below. The only solution I can think of is to put your default code in cases and add break .

In addition, you do not need to break in the default case, since its last case is in the switch block.

0
Nov 11 2018-10-11
source share

I really don't see what you want.

  • If you want to start the material by default in all cases, just put it after switching.
  • If you want to run the material by default only in the "created" case and in the default case, change the position of the "created" and "creator" sections and place the gap after the first.
  • If you want this code to be executed only when creating or creating matches, then get rid of the switch statement and use if / else OR use the flag and the following if statement.

All the tools are there.

0
Nov 11 2018-10-11
source share



All Articles