PHP 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 is 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 |
scalar boolean false | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, bool given |
scalar boolean false | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, bool given |
scalar boolean false | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, bool given |
scalar boolean false | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, bool given |
scalar boolean false | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, bool given |
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 |
scalar boolean true | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, bool given |
scalar boolean true | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, bool given |
scalar boolean true | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, bool given |
scalar boolean true | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, bool given |
scalar boolean true | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, bool given |
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 |
scalar integer 0 | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, int given |
scalar integer 0 | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, int given |
scalar integer 0 | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, int given |
scalar integer 0 | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, int given |
scalar integer 0 | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, int given |
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 |
scalar integer 1 | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, int given |
scalar integer 1 | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, int given |
scalar integer 1 | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, int given |
scalar integer 1 | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, int given |
scalar integer 1 | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, int given |
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 |
scalar float 1.1 | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, float given |
scalar float 1.1 | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, float given |
scalar float 1.1 | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, float given |
scalar float 1.1 | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, float given |
scalar float 1.1 | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, float given |
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 |
scalar string empty | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, string given |
scalar string empty | string | string | |
scalar string empty | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, string given |
scalar string empty | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, string given |
scalar string empty | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, string given |
scalar string empty | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, string given |
scalar string empty | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, string given |
scalar string empty | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, string given |
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 |
scalar string string | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, string given |
scalar string string | string | string | |
scalar string string | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, string given |
scalar string string | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, string given |
scalar string string | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, string given |
scalar string string | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, string given |
scalar string string | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, string given |
scalar string string | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, string given |
scalar string string | nullable_string | string | |
compound array indexed | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, array given |
compound array indexed | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, array given |
compound array indexed | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, array given |
compound array indexed | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, array given |
compound array indexed | array | array | |
compound array indexed | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, array given |
compound array indexed | iterable | array | |
compound array indexed | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, array given |
compound array indexed | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, array given |
compound array indexed | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, array given |
compound array indexed | nullable_string | TypeError | TypeDeclarationTester::test_nullable_string(): Argument #1 ($value) must be of type ?string, array given |
compound array associative | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, array given |
compound array associative | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, array given |
compound array associative | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, array given |
compound array associative | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, array given |
compound array associative | array | array | |
compound array associative | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, array given |
compound array associative | iterable | array | |
compound array associative | object | TypeError | TypeDeclarationTester::test_object(): Argument #1 ($value) must be of type ?object, array given |
compound array associative | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, array given |
compound array associative | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, array given |
compound array associative | nullable_string | TypeError | TypeDeclarationTester::test_nullable_string(): Argument #1 ($value) must be of type ?string, array given |
compound object TypeDeclarationTester | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, TypeDeclarationTester given |
compound object TypeDeclarationTester | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, TypeDeclarationTester given |
compound object TypeDeclarationTester | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, TypeDeclarationTester given |
compound object TypeDeclarationTester | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, TypeDeclarationTester given |
compound object TypeDeclarationTester | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, TypeDeclarationTester given |
compound object TypeDeclarationTester | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, TypeDeclarationTester given |
compound object TypeDeclarationTester | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, TypeDeclarationTester given |
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 |
compound object TypeDeclarationParent | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, TypeDeclarationParent given |
compound object TypeDeclarationParent | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, TypeDeclarationParent given |
compound object TypeDeclarationParent | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, TypeDeclarationParent given |
compound object TypeDeclarationParent | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, TypeDeclarationParent given |
compound object TypeDeclarationParent | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, TypeDeclarationParent given |
compound object TypeDeclarationParent | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, TypeDeclarationParent given |
compound object TypeDeclarationParent | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, TypeDeclarationParent given |
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 |
compound object TypeDeclarationParent | nullable_string | TypeError | TypeDeclarationTester::test_nullable_string(): Argument #1 ($value) must be of type ?string, TypeDeclarationParent given |
compound object self | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, TypeDeclarationTester given |
compound object self | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, TypeDeclarationTester given |
compound object self | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, TypeDeclarationTester given |
compound object self | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, TypeDeclarationTester given |
compound object self | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, TypeDeclarationTester given |
compound object self | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, TypeDeclarationTester given |
compound object self | iterable | TypeError | TypeDeclarationTester::test_iterable(): Argument #1 ($value) must be of type ?iterable, TypeDeclarationTester given |
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 |
compound callable function | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, Closure given |
compound callable function | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, Closure given |
compound callable function | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, Closure given |
compound callable function | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, Closure given |
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 |
compound callable function | object | object | |
compound callable function | parent | TypeError | TypeDeclarationTester::test_parent(): Argument #1 ($value) must be of type TypeDeclarationParent, Closure given |
compound callable function | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, Closure given |
compound callable function | nullable_string | TypeError | TypeDeclarationTester::test_nullable_string(): Argument #1 ($value) must be of type ?string, Closure given |
special null | boolean | TypeError | TypeDeclarationTester::test_boolean(): Argument #1 ($value) must be of type bool, null given |
special null | float | TypeError | TypeDeclarationTester::test_float(): Argument #1 ($value) must be of type float, null given |
special null | integer | TypeError | TypeDeclarationTester::test_integer(): Argument #1 ($value) must be of type int, null given |
special null | string | TypeError | TypeDeclarationTester::test_string(): Argument #1 ($value) must be of type string, null given |
special null | array | TypeError | TypeDeclarationTester::test_array(): Argument #1 ($value) must be of type array, null given |
special null | callable | TypeError | TypeDeclarationTester::test_callable(): Argument #1 ($value) must be of type callable, null given |
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 |
special null | self | TypeError | TypeDeclarationTester::test_self(): Argument #1 ($value) must be of type TypeDeclarationTester, null given |
special null | nullable_string | NULL |
Leave a Reply