Skip to content

Instantly share code, notes, and snippets.

@volter9
Created January 3, 2015 18:37
Show Gist options
  • Save volter9/30dd7281613fdc7686b7 to your computer and use it in GitHub Desktop.
Save volter9/30dd7281613fdc7686b7 to your computer and use it in GitHub Desktop.
Handling scalar PHP type hinting
/**
* @link http://php.net/manual/en/language.oop5.typehinting.php#111411
*/
function optimized_strpos ($ErrLevel, $ErrMessage) {
if ($ErrLevel == E_RECOVERABLE_ERROR)
return strpos($ErrMessage, 'must be an instance of string, string')
|| strpos($ErrMessage, 'must be an instance of integer, integer')
|| strpos($ErrMessage, 'must be an instance of float, double')
|| strpos($ErrMessage, 'must be an instance of boolean, boolean')
|| strpos($ErrMessage, 'must be an instance of resource, resource');
}
set_error_handler('optimized_strpos');
function test (string $name) {
echo $name;
}
test('Hello!'); // Hello!
test(1); // Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment