Created
January 3, 2015 18:37
-
-
Save volter9/30dd7281613fdc7686b7 to your computer and use it in GitHub Desktop.
Handling scalar PHP type hinting
This file contains hidden or 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
/** | |
* @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