Parameter type declarations and return type declarations were added to PHP in 7.0
For scalar types (bool, int, float, string), what happens when a different type of value is passed into a function depends on if strict mode is enabled in the file that calls the function. The default is to coerce some scalar values of the wrong type into the expected declared type. If strict mode in enabled in the file that called the function, a TypeError is thrown when passing a parameter of the wrong type. Only the mode in the file that calls the function matters. Strict mode does not matter in files farther back in the stack trace.
When a function with a declared return type returns a value with a different type, a TypeError is thrown regardless of strict mode.
Passed Type and Value | Declared Type | What Type Does the Function Get? | Error Message |
---|---|---|---|
scalar boolean false | boolean | boolean | |
scalar boolean false | float | double | |
scalar boolean false | integer | integer | |
scalar boolean false | string | string | |
scalar boolean false | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean false | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean false | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean false | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean false | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean false | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean false | nullable_string | string | |
scalar boolean true | boolean | boolean | |
scalar boolean true | float | double | |
scalar boolean true | integer | integer | |
scalar boolean true | string | string | |
scalar boolean true | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean true | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean true | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean true | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean true | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean true | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, bool given, called in TypeDeclarations.php on line 133 |
scalar boolean true | nullable_string | string | |
scalar integer 0 | boolean | boolean | |
scalar integer 0 | float | double | |
scalar integer 0 | integer | integer | |
scalar integer 0 | string | string | |
scalar integer 0 | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, int given, called in TypeDeclarations.php on line 133 |
scalar integer 0 | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, int given, called in TypeDeclarations.php on line 133 |
scalar integer 0 | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, int given, called in TypeDeclarations.php on line 133 |
scalar integer 0 | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, int given, called in TypeDeclarations.php on line 133 |
scalar integer 0 | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, int given, called in TypeDeclarations.php on line 133 |
scalar integer 0 | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, int given, called in TypeDeclarations.php on line 133 |
scalar integer 0 | nullable_string | string | |
scalar integer 1 | boolean | boolean | |
scalar integer 1 | float | double | |
scalar integer 1 | integer | integer | |
scalar integer 1 | string | string | |
scalar integer 1 | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, int given, called in TypeDeclarations.php on line 133 |
scalar integer 1 | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, int given, called in TypeDeclarations.php on line 133 |
scalar integer 1 | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, int given, called in TypeDeclarations.php on line 133 |
scalar integer 1 | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, int given, called in TypeDeclarations.php on line 133 |
scalar integer 1 | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, int given, called in TypeDeclarations.php on line 133 |
scalar integer 1 | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, int given, called in TypeDeclarations.php on line 133 |
scalar integer 1 | nullable_string | string | |
scalar float 1.1 | boolean | boolean | |
scalar float 1.1 | float | double | |
scalar float 1.1 | integer | integer | |
scalar float 1.1 | string | string | |
scalar float 1.1 | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, float given, called in TypeDeclarations.php on line 133 |
scalar float 1.1 | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, float given, called in TypeDeclarations.php on line 133 |
scalar float 1.1 | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, float given, called in TypeDeclarations.php on line 133 |
scalar float 1.1 | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, float given, called in TypeDeclarations.php on line 133 |
scalar float 1.1 | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, float given, called in TypeDeclarations.php on line 133 |
scalar float 1.1 | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, float given, called in TypeDeclarations.php on line 133 |
scalar float 1.1 | nullable_string | string | |
scalar string empty | boolean | boolean | |
scalar string empty | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, string given, called in TypeDeclarations.php on line 133 |
scalar string empty | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, string given, called in TypeDeclarations.php on line 133 |
scalar string empty | string | string | |
scalar string empty | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, string given, called in TypeDeclarations.php on line 133 |
scalar string empty | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, string given, called in TypeDeclarations.php on line 133 |
scalar string empty | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, string given, called in TypeDeclarations.php on line 133 |
scalar string empty | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, string given, called in TypeDeclarations.php on line 133 |
scalar string empty | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, string given, called in TypeDeclarations.php on line 133 |
scalar string empty | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, string given, called in TypeDeclarations.php on line 133 |
scalar string empty | nullable_string | string | |
scalar string string | boolean | boolean | |
scalar string string | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, string given, called in TypeDeclarations.php on line 133 |
scalar string string | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, string given, called in TypeDeclarations.php on line 133 |
scalar string string | string | string | |
scalar string string | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, string given, called in TypeDeclarations.php on line 133 |
scalar string string | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, string given, called in TypeDeclarations.php on line 133 |
scalar string string | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, string given, called in TypeDeclarations.php on line 133 |
scalar string string | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, string given, called in TypeDeclarations.php on line 133 |
scalar string string | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, string given, called in TypeDeclarations.php on line 133 |
scalar string string | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, string given, called in TypeDeclarations.php on line 133 |
scalar string string | nullable_string | string | |
compound array indexed | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, array given, called in TypeDeclarations.php on line 133 |
compound array indexed | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, array given, called in TypeDeclarations.php on line 133 |
compound array indexed | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, array given, called in TypeDeclarations.php on line 133 |
compound array indexed | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, array given, called in TypeDeclarations.php on line 133 |
compound array indexed | array | array | |
compound array indexed | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, array given, called in TypeDeclarations.php on line 133 |
compound array indexed | iterable | array | |
compound array indexed | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, array given, called in TypeDeclarations.php on line 133 |
compound array indexed | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, array given, called in TypeDeclarations.php on line 133 |
compound array indexed | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, array given, called in TypeDeclarations.php on line 133 |
compound array indexed | nullable_string | TypeError | TypeDeclarationTester::test_nullable_string(): Argument #1 ($value) must be of type ?string, array given, called in TypeDeclarations.php on line 133 |
compound array associative | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, array given, called in TypeDeclarations.php on line 133 |
compound array associative | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, array given, called in TypeDeclarations.php on line 133 |
compound array associative | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, array given, called in TypeDeclarations.php on line 133 |
compound array associative | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, array given, called in TypeDeclarations.php on line 133 |
compound array associative | array | array | |
compound array associative | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, array given, called in TypeDeclarations.php on line 133 |
compound array associative | iterable | array | |
compound array associative | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, array given, called in TypeDeclarations.php on line 133 |
compound array associative | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, array given, called in TypeDeclarations.php on line 133 |
compound array associative | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, array given, called in TypeDeclarations.php on line 133 |
compound array associative | nullable_string | TypeError | TypeDeclarationTester::test_nullable_string(): Argument #1 ($value) must be of type ?string, array given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationTester | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationTester | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationTester | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationTester | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationTester | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationTester | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationTester | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationTester | object | object | |
compound object TypeDeclarationTester | parent | object | |
compound object TypeDeclarationTester | self | object | |
compound object TypeDeclarationTester | nullable_string | TypeError | TypeDeclarationTester::test_nullable_string(): Argument #1 ($value) must be of type ?string, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationParent | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, TypeDeclarationParent given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationParent | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, TypeDeclarationParent given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationParent | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, TypeDeclarationParent given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationParent | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, TypeDeclarationParent given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationParent | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, TypeDeclarationParent given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationParent | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, TypeDeclarationParent given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationParent | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, TypeDeclarationParent given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationParent | object | object | |
compound object TypeDeclarationParent | parent | object | |
compound object TypeDeclarationParent | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, TypeDeclarationParent given, called in TypeDeclarations.php on line 133 |
compound object TypeDeclarationParent | nullable_string | TypeError | TypeDeclarationTester::test_nullable_string(): Argument #1 ($value) must be of type ?string, TypeDeclarationParent given, called in TypeDeclarations.php on line 133 |
compound object self | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object self | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object self | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object self | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object self | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object self | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object self | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound object self | object | object | |
compound object self | parent | object | |
compound object self | self | object | |
compound object self | nullable_string | TypeError | TypeDeclarationTester::test_nullable_string(): Argument #1 ($value) must be of type ?string, TypeDeclarationTester given, called in TypeDeclarations.php on line 133 |
compound callable function | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, Closure given, called in TypeDeclarations.php on line 133 |
compound callable function | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, Closure given, called in TypeDeclarations.php on line 133 |
compound callable function | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, Closure given, called in TypeDeclarations.php on line 133 |
compound callable function | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, Closure given, called in TypeDeclarations.php on line 133 |
compound callable function | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) TypeDeclarations.php on line 133 |
compound callable function | callable | object | |
compound callable function | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, Closure given, called in TypeDeclarations.php on line 133 |
compound callable function | object | object | |
compound callable function | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, Closure given, called in TypeDeclarations.php on line 133 |
compound callable function | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, Closure given, called in TypeDeclarations.php on line 133 |
compound callable function | nullable_string | TypeError | TypeDeclarationTester::test_nullable_string(): Argument #1 ($value) must be of type ?string, Closure given, called in TypeDeclarations.php on line 133 |
special null | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, null given, called in TypeDeclarations.php on line 133 |
special null | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, null given, called in TypeDeclarations.php on line 133 |
special null | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, null given, called in TypeDeclarations.php on line 133 |
special null | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, null given, called in TypeDeclarations.php on line 133 |
special null | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, null given, called in TypeDeclarations.php on line 133 |
special null | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, null given, called in TypeDeclarations.php on line 133 |
special null | iterable | NULL | |
special null | object | NULL | |
special null | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, null given, called in TypeDeclarations.php on line 133 |
special null | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, null given, called in TypeDeclarations.php on line 133 |
special null | nullable_string | NULL |