Created
October 10, 2013 04:30
-
-
Save SchumacherFM/6913061 to your computer and use it in GitHub Desktop.
The always nice and awesome and total cool Switch Statements :-(
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Returns a PDO parameter constant to bind a variable with | |
* | |
* @param string $type Which variable type to check | |
* | |
* @return int PDO parameter constant | |
*/ | |
public static function getBindConstant($type) | |
{ | |
switch ($type) { | |
case 'bool': | |
return PDO::PARAM_BOOL; | |
break; | |
case 'int': | |
return PDO::PARAM_INT; | |
break; | |
case 'null': | |
return PDO::PARAM_NULL; | |
break; | |
case 'string': | |
default: | |
return PDO::PARAM_STR; | |
break; | |
} | |
} | |
// refactored: | |
public static function getBindConstant($type) | |
{ | |
$match = [ | |
'bool' => PDO::PARAM_BOOL, | |
'int' => PDO::PARAM_INT, | |
'null' => PDO::PARAM_NULL, | |
'string' => PDO::PARAM_STR, | |
]; | |
return isset($match[$type]) ? $match[$type] : PDO::PARAM_STR; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment