As you're probably aware, PHP doesn't support type hinting for scalar types.I feel this might be good practice to build into my framework, although I'm aware some of you might disagree seeing as PHP is a loosely-typed language.But I'd like some feedback on the implementation of this.Do you think it's efficient, pointless? Or could it be achieved in a better way?This works by causing an error.By specifying a typehint (integer), PHP is expecting a class/object called integer, so it's giving an error as we are passing a real integer/scalar value.Giving this error,Argument 1 passed to test() must be an instance of integer, integer given.So with a custom error handler function, I can use preg_match() to find the expected & given types, and just compare them via the name.The code<?phpini_set('display_errors', 'on');error_reporting(E_ALL);function typehint($level, $message){    if($level == E_RECOVERABLE_ERROR){         if(preg_match('/^Argument (\d)+ passed to (?:(\w+)::)?(\w+)\(\) must be an instance of (\w+), (\w+) given/', $message, $match)){             if($match[4] == $match[5]){                return true;             }        }                     }     return false; } set_error_handler('typehint');function test(integer $value){    var_dump($value);   }for($int = 0; $int < 1000; $int++){    test(23);}?>It seems to work ok, but I'm not sure if using preg_match() on every function argument is a good idea?Here's the benchmarks, on 1000 function calls. (1 typeint per function)Type-hinting        No Type-hinting0.015               0.00170.0169              0.0020.0172              0.0020.0148              0.00190.0159              0.00290.0157              0.00190.0149              0.00180.0161              0.00290.0167              0.0020.0173              0.00180.0149              0.00190.0156              0.00190.0153              0.00290.0182              0.00230.0151              0.00290.0163              0.00170.0179              0.00180.0159              0.00180.0154              0.00270.0149              0.0023Average             Average0.016               0.002155So just after some thoughts on this really... good or bad?EDIT:Using strstr() seems to be a little quicker, although I have to specify what types to check for. Which is a small number anyway. Average time: 0.009595.function typehint($level, $message){    if($level == E_RECOVERABLE_ERROR){         $integer = 'must be an instance of integer, integer given';        $string = 'must be an instance of string, string given';        if(strstr($message, $integer) !== false OR strstr($message, $string) !== false){            return true;        }    }     return false; } 
Type hinting for scalar types
php