Skip to content

Instantly share code, notes, and snippets.

@miholeus
Created April 13, 2012 19:46
Show Gist options
  • Save miholeus/2379592 to your computer and use it in GitHub Desktop.
Save miholeus/2379592 to your computer and use it in GitHub Desktop.
PHP type hinting
set_error_handler('handle');
function foo(integer $p1, bool $p2) {
echo "ok" . PHP_EOL;
}
$param1 = 123;
$param2 = 'string val!';
foo($param1, $param2);
function handle($lvl, $msg)
{
static $_types = array(
'boolean,' => 'boolean',
'bool,' => 'boolean',
'integer,' => 'integer',
'int,' => 'integer',
'float,' => 'float',
'double,' => 'float',
'real,' => 'float',
'string,' => 'string',
'resource,' => 'resource'
);
if ($lvl == E_RECOVERABLE_ERROR) {
$errmsg = explode(' ', $msg, 13);
if (isset($_types[$errmsg[10]]) && $_types[$errmsg[10]] == $errmsg[11]) {
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment