Little switch improvement: get the used variable

From: Date: Tue, 23 Sep 2014 07:05:18 +0000
Subject: Little switch improvement: get the used variable
Groups: php.internals 
Request: Send a blank email to internals+get-77502@lists.php.net to get a copy of this message
Hello together, i often write switches, where i throw an Exception when something unknown come in, rather than using a default value. switch($myVar){ case 1: doSomethingElse(); break; case 2: doSomething(); break; //... default: throw new Exception('Undefined input: ' . $myVar); break; } I would like to write something like this: switch($myVar){ //... default: throw new Exception('Undefined input: ' . get_the_used_switch_variable()); break; } The get_the_used_switch_variable() is just a placeholder, name can be changed to something natural...maybe a constant. It's really usefull, when dealing with nested objects like when you work with Doctrine. Best regards Martin

« previous php.internals (#77502) next »