3v4l.org

run code in 300+ PHP versions simultaneously
<?php define('TYPEHINT_PCRE', '/^Argument (\d)+ passed to (?:(\w+)::)?(\w+)\(\) must be an instance of (\w+), (\w+) given/'); class Typehint { private static $Typehints = array( 'boolean' => 'is_bool', 'int' => 'is_int', 'integer' => 'is_int', 'float' => 'is_float', 'string' => 'is_string', 'resrouce' => 'is_resource' ); private function __Constrct() {} public static function initializeHandler() { set_error_handler('Typehint::handleTypehint', E_ALL); return TRUE; } private static function getTypehintedArgument($ThBackTrace, $ThFunction, $ThArgIndex, &$ThArgValue) { foreach ($ThBackTrace as $ThTrace) { // Match the function; Note we could do more defensive error checking. if (isset($ThTrace['function']) && $ThTrace['function'] == $ThFunction) { $ThArgValue = $ThTrace['args'][$ThArgIndex - 1]; return TRUE; } } return FALSE; } public static function handleTypehint($ErrLevel, $ErrMessage, $ErrFile, $ErrLine) { if ($ErrLevel == E_RECOVERABLE_ERROR) { if (preg_match(TYPEHINT_PCRE, $ErrMessage, $ErrMatches)) { list($ErrMatch, $ThArgIndex, $ThClass, $ThFunction, $ThHint, $ThType) = $ErrMatches; if (isset(self::$Typehints[$ThHint])) { $ThBacktrace = debug_backtrace(); $ThArgValue = NULL; if (self::getTypehintedArgument($ThBacktrace, $ThFunction, $ThArgIndex, $ThArgValue)) { if (call_user_func(self::$Typehints[$ThHint], $ThArgValue)) { return TRUE; } } } } } throw new ErrorException($ErrMessage, 0, $ErrLevel, $ErrFile, $ErrLine); } } Typehint::initializeHandler(); function test(int $number) { echo $number; } test(5); test("5");
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
55
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
5 Fatal error: Uncaught exception 'ErrorException' with message 'Argument 1 passed to test() must be an instance of int, string given, called in /in/oEYJC on line 88 and defined' in /in/oEYJC:83 Stack trace: #0 /in/oEYJC(83): Typehint::handleTypehint(4096, 'Argument 1 pass...', '/in/oEYJC', 83, Array) #1 /in/oEYJC(88): test('5') #2 {main} thrown in /in/oEYJC on line 83
Process exited with code 255.
Output for 5.2.2 - 5.2.17
5 Fatal error: Uncaught exception 'ErrorException' with message 'Argument 1 passed to test() must be an instance of int, string given, called in /in/oEYJC on line 88 and defined' in /in/oEYJC:83 Stack trace: #0 /in/oEYJC(83): Typehint::handleTypehint('5') #1 /in/oEYJC(88): test() #2 {main} thrown in /in/oEYJC on line 83
Process exited with code 255.
Output for 5.2.0 - 5.2.1
Catchable fatal error: Argument 1 passed to test() must be an instance of int, integer given, called in /in/oEYJC on line 87 and defined in /in/oEYJC on line 83
Process exited with code 255.
Output for 5.1.0 - 5.1.6
Fatal error: Argument 1 passed to test() must be an object of class int, called in /in/oEYJC on line 87 and defined in /in/oEYJC on line 83
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Warning: set_error_handler() expects the argument (Typehint::handleTypehint) to be a valid callback in /in/oEYJC on line 22 Fatal error: Argument 1 must be an object of class int in /in/oEYJC on line 83
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/oEYJC on line 8
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/oEYJC on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/oEYJC on line 8
Process exited with code 255.

preferences:
235.26 ms | 401 KiB | 355 Q