Parameter Type Declarations

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 ValueDeclared TypeWhat Type Does the Function Get?Error Message
scalar boolean falsebooleanboolean
scalar boolean falsefloatdouble
scalar boolean falseintegerinteger
scalar boolean falsestringstring
scalar boolean falsearrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) must be of type array,
bool given
scalar boolean falsecallableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
bool given
scalar boolean falseiterableTypeErrorTypeDeclarationTester::test_iterable():
Argument #1 ($value) must be of type ?iterable,
bool given
scalar boolean falseobjectTypeErrorTypeDeclarationTester::test_object():
Argument #1 ($value) must be of type ?object,
bool given
scalar boolean falseparentTypeErrorTypeDeclarationTester::test_parent():
Argument #1 ($value) must be of type TypeDeclarationParent,
bool given
scalar boolean falseselfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
bool given
scalar boolean falsenullable_stringstring
scalar boolean truebooleanboolean
scalar boolean truefloatdouble
scalar boolean trueintegerinteger
scalar boolean truestringstring
scalar boolean truearrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) must be of type array,
bool given
scalar boolean truecallableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
bool given
scalar boolean trueiterableTypeErrorTypeDeclarationTester::test_iterable():
Argument #1 ($value) must be of type ?iterable,
bool given
scalar boolean trueobjectTypeErrorTypeDeclarationTester::test_object():
Argument #1 ($value) must be of type ?object,
bool given
scalar boolean trueparentTypeErrorTypeDeclarationTester::test_parent():
Argument #1 ($value) must be of type TypeDeclarationParent,
bool given
scalar boolean trueselfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
bool given
scalar boolean truenullable_stringstring
scalar integer 0booleanboolean
scalar integer 0floatdouble
scalar integer 0integerinteger
scalar integer 0stringstring
scalar integer 0arrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) must be of type array,
int given
scalar integer 0callableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
int given
scalar integer 0iterableTypeErrorTypeDeclarationTester::test_iterable():
Argument #1 ($value) must be of type ?iterable,
int given
scalar integer 0objectTypeErrorTypeDeclarationTester::test_object():
Argument #1 ($value) must be of type ?object,
int given
scalar integer 0parentTypeErrorTypeDeclarationTester::test_parent():
Argument #1 ($value) must be of type TypeDeclarationParent,
int given
scalar integer 0selfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
int given
scalar integer 0nullable_stringstring
scalar integer 1booleanboolean
scalar integer 1floatdouble
scalar integer 1integerinteger
scalar integer 1stringstring
scalar integer 1arrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) must be of type array,
int given
scalar integer 1callableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
int given
scalar integer 1iterableTypeErrorTypeDeclarationTester::test_iterable():
Argument #1 ($value) must be of type ?iterable,
int given
scalar integer 1objectTypeErrorTypeDeclarationTester::test_object():
Argument #1 ($value) must be of type ?object,
int given
scalar integer 1parentTypeErrorTypeDeclarationTester::test_parent():
Argument #1 ($value) must be of type TypeDeclarationParent,
int given
scalar integer 1selfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
int given
scalar integer 1nullable_stringstring
scalar float 1.1booleanboolean
scalar float 1.1floatdouble
scalar float 1.1integerinteger
scalar float 1.1stringstring
scalar float 1.1arrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) must be of type array,
float given
scalar float 1.1callableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
float given
scalar float 1.1iterableTypeErrorTypeDeclarationTester::test_iterable():
Argument #1 ($value) must be of type ?iterable,
float given
scalar float 1.1objectTypeErrorTypeDeclarationTester::test_object():
Argument #1 ($value) must be of type ?object,
float given
scalar float 1.1parentTypeErrorTypeDeclarationTester::test_parent():
Argument #1 ($value) must be of type TypeDeclarationParent,
float given
scalar float 1.1selfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
float given
scalar float 1.1nullable_stringstring
scalar string emptybooleanboolean
scalar string emptyfloatTypeErrorTypeDeclarationTester::test_float():
Argument #1 ($value) must be of type float,
string given
scalar string emptyintegerTypeErrorTypeDeclarationTester::test_integer():
Argument #1 ($value) must be of type int,
string given
scalar string emptystringstring
scalar string emptyarrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) must be of type array,
string given
scalar string emptycallableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
string given
scalar string emptyiterableTypeErrorTypeDeclarationTester::test_iterable():
Argument #1 ($value) must be of type ?iterable,
string given
scalar string emptyobjectTypeErrorTypeDeclarationTester::test_object():
Argument #1 ($value) must be of type ?object,
string given
scalar string emptyparentTypeErrorTypeDeclarationTester::test_parent():
Argument #1 ($value) must be of type TypeDeclarationParent,
string given
scalar string emptyselfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
string given
scalar string emptynullable_stringstring
scalar string stringbooleanboolean
scalar string stringfloatTypeErrorTypeDeclarationTester::test_float():
Argument #1 ($value) must be of type float,
string given
scalar string stringintegerTypeErrorTypeDeclarationTester::test_integer():
Argument #1 ($value) must be of type int,
string given
scalar string stringstringstring
scalar string stringarrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) must be of type array,
string given
scalar string stringcallableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
string given
scalar string stringiterableTypeErrorTypeDeclarationTester::test_iterable():
Argument #1 ($value) must be of type ?iterable,
string given
scalar string stringobjectTypeErrorTypeDeclarationTester::test_object():
Argument #1 ($value) must be of type ?object,
string given
scalar string stringparentTypeErrorTypeDeclarationTester::test_parent():
Argument #1 ($value) must be of type TypeDeclarationParent,
string given
scalar string stringselfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
string given
scalar string stringnullable_stringstring
compound array indexedbooleanTypeErrorTypeDeclarationTester::test_boolean():
Argument #1 ($value) must be of type bool,
array given
compound array indexedfloatTypeErrorTypeDeclarationTester::test_float():
Argument #1 ($value) must be of type float,
array given
compound array indexedintegerTypeErrorTypeDeclarationTester::test_integer():
Argument #1 ($value) must be of type int,
array given
compound array indexedstringTypeErrorTypeDeclarationTester::test_string():
Argument #1 ($value) must be of type string,
array given
compound array indexedarrayarray
compound array indexedcallableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
array given
compound array indexediterablearray
compound array indexedobjectTypeErrorTypeDeclarationTester::test_object():
Argument #1 ($value) must be of type ?object,
array given
compound array indexedparentTypeErrorTypeDeclarationTester::test_parent():
Argument #1 ($value) must be of type TypeDeclarationParent,
array given
compound array indexedselfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
array given
compound array indexednullable_stringTypeErrorTypeDeclarationTester::test_nullable_string():
Argument #1 ($value) must be of type ?string,
array given
compound array associativebooleanTypeErrorTypeDeclarationTester::test_boolean():
Argument #1 ($value) must be of type bool,
array given
compound array associativefloatTypeErrorTypeDeclarationTester::test_float():
Argument #1 ($value) must be of type float,
array given
compound array associativeintegerTypeErrorTypeDeclarationTester::test_integer():
Argument #1 ($value) must be of type int,
array given
compound array associativestringTypeErrorTypeDeclarationTester::test_string():
Argument #1 ($value) must be of type string,
array given
compound array associativearrayarray
compound array associativecallableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
array given
compound array associativeiterablearray
compound array associativeobjectTypeErrorTypeDeclarationTester::test_object():
Argument #1 ($value) must be of type ?object,
array given
compound array associativeparentTypeErrorTypeDeclarationTester::test_parent():
Argument #1 ($value) must be of type TypeDeclarationParent,
array given
compound array associativeselfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
array given
compound array associativenullable_stringTypeErrorTypeDeclarationTester::test_nullable_string():
Argument #1 ($value) must be of type ?string,
array given
compound object TypeDeclarationTesterbooleanTypeErrorTypeDeclarationTester::test_boolean():
Argument #1 ($value) must be of type bool,
TypeDeclarationTester given
compound object TypeDeclarationTesterfloatTypeErrorTypeDeclarationTester::test_float():
Argument #1 ($value) must be of type float,
TypeDeclarationTester given
compound object TypeDeclarationTesterintegerTypeErrorTypeDeclarationTester::test_integer():
Argument #1 ($value) must be of type int,
TypeDeclarationTester given
compound object TypeDeclarationTesterstringTypeErrorTypeDeclarationTester::test_string():
Argument #1 ($value) must be of type string,
TypeDeclarationTester given
compound object TypeDeclarationTesterarrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) must be of type array,
TypeDeclarationTester given
compound object TypeDeclarationTestercallableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
TypeDeclarationTester given
compound object TypeDeclarationTesteriterableTypeErrorTypeDeclarationTester::test_iterable():
Argument #1 ($value) must be of type ?iterable,
TypeDeclarationTester given
compound object TypeDeclarationTesterobjectobject
compound object TypeDeclarationTesterparentobject
compound object TypeDeclarationTesterselfobject
compound object TypeDeclarationTesternullable_stringTypeErrorTypeDeclarationTester::test_nullable_string():
Argument #1 ($value) must be of type ?string,
TypeDeclarationTester given
compound object TypeDeclarationParentbooleanTypeErrorTypeDeclarationTester::test_boolean():
Argument #1 ($value) must be of type bool,
TypeDeclarationParent given
compound object TypeDeclarationParentfloatTypeErrorTypeDeclarationTester::test_float():
Argument #1 ($value) must be of type float,
TypeDeclarationParent given
compound object TypeDeclarationParentintegerTypeErrorTypeDeclarationTester::test_integer():
Argument #1 ($value) must be of type int,
TypeDeclarationParent given
compound object TypeDeclarationParentstringTypeErrorTypeDeclarationTester::test_string():
Argument #1 ($value) must be of type string,
TypeDeclarationParent given
compound object TypeDeclarationParentarrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) must be of type array,
TypeDeclarationParent given
compound object TypeDeclarationParentcallableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
TypeDeclarationParent given
compound object TypeDeclarationParentiterableTypeErrorTypeDeclarationTester::test_iterable():
Argument #1 ($value) must be of type ?iterable,
TypeDeclarationParent given
compound object TypeDeclarationParentobjectobject
compound object TypeDeclarationParentparentobject
compound object TypeDeclarationParentselfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
TypeDeclarationParent given
compound object TypeDeclarationParentnullable_stringTypeErrorTypeDeclarationTester::test_nullable_string():
Argument #1 ($value) must be of type ?string,
TypeDeclarationParent given
compound object selfbooleanTypeErrorTypeDeclarationTester::test_boolean():
Argument #1 ($value) must be of type bool,
TypeDeclarationTester given
compound object selffloatTypeErrorTypeDeclarationTester::test_float():
Argument #1 ($value) must be of type float,
TypeDeclarationTester given
compound object selfintegerTypeErrorTypeDeclarationTester::test_integer():
Argument #1 ($value) must be of type int,
TypeDeclarationTester given
compound object selfstringTypeErrorTypeDeclarationTester::test_string():
Argument #1 ($value) must be of type string,
TypeDeclarationTester given
compound object selfarrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) must be of type array,
TypeDeclarationTester given
compound object selfcallableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
TypeDeclarationTester given
compound object selfiterableTypeErrorTypeDeclarationTester::test_iterable():
Argument #1 ($value) must be of type ?iterable,
TypeDeclarationTester given
compound object selfobjectobject
compound object selfparentobject
compound object selfselfobject
compound object selfnullable_stringTypeErrorTypeDeclarationTester::test_nullable_string():
Argument #1 ($value) must be of type ?string,
TypeDeclarationTester given
compound callable functionbooleanTypeErrorTypeDeclarationTester::test_boolean():
Argument #1 ($value) must be of type bool,
Closure given
compound callable functionfloatTypeErrorTypeDeclarationTester::test_float():
Argument #1 ($value) must be of type float,
Closure given
compound callable functionintegerTypeErrorTypeDeclarationTester::test_integer():
Argument #1 ($value) must be of type int,
Closure given
compound callable functionstringTypeErrorTypeDeclarationTester::test_string():
Argument #1 ($value) must be of type string,
Closure given
compound callable functionarrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) TypeDeclarations.php on line 133
compound callable functioncallableobject
compound callable functioniterableTypeErrorTypeDeclarationTester::test_iterable():
Argument #1 ($value) must be of type ?iterable,
Closure given
compound callable functionobjectobject
compound callable functionparentTypeErrorTypeDeclarationTester::test_parent():
Argument #1 ($value) must be of type TypeDeclarationParent,
Closure given
compound callable functionselfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
Closure given
compound callable functionnullable_stringTypeErrorTypeDeclarationTester::test_nullable_string():
Argument #1 ($value) must be of type ?string,
Closure given
special nullbooleanTypeErrorTypeDeclarationTester::test_boolean():
Argument #1 ($value) must be of type bool,
null given
special nullfloatTypeErrorTypeDeclarationTester::test_float():
Argument #1 ($value) must be of type float,
null given
special nullintegerTypeErrorTypeDeclarationTester::test_integer():
Argument #1 ($value) must be of type int,
null given
special nullstringTypeErrorTypeDeclarationTester::test_string():
Argument #1 ($value) must be of type string,
null given
special nullarrayTypeErrorTypeDeclarationTester::test_array():
Argument #1 ($value) must be of type array,
null given
special nullcallableTypeErrorTypeDeclarationTester::test_callable():
Argument #1 ($value) must be of type callable,
null given
special nulliterableNULL
special nullobjectNULL
special nullparentTypeErrorTypeDeclarationTester::test_parent():
Argument #1 ($value) must be of type TypeDeclarationParent,
null given
special nullselfTypeErrorTypeDeclarationTester::test_self():
Argument #1 ($value) must be of type TypeDeclarationTester,
null given
special nullnullable_stringNULL

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.