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".
php switch-statement
Rene Terstegen Nov 11 2018-10-11 14:41
source share