Skip to content

Instantly share code, notes, and snippets.

@SchumacherFM
Created October 10, 2013 04:30
Show Gist options
  • Save SchumacherFM/6913061 to your computer and use it in GitHub Desktop.
Save SchumacherFM/6913061 to your computer and use it in GitHub Desktop.
The always nice and awesome and total cool Switch Statements :-(
<?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