PHP Parameter Type Declarations

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

Leave a Reply

Your email address will not be published.