php: Declare arguments type of a Function
Solution 1
According to the PHP5 documentation:
Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported.
Since string
and int
are not classes, you can't "type-hint" them in your function.
As of PHP 7.0 declaring argument type as string, int, float, bool is supported.
Solution 2
This maybe useful for anyone who see this post since the availability of PHP 7
With PHP 7, its now possible to declare types. You can refer the following link for more information.
http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
function(string $name, bool $is_admin) {
//do something
}
Solution 3
According to PHP Manual, you can do that for array
on PHP 5.1 and beyond and for string
and int
types on PHP 7 and beyond. Take a look:
-
Class/interface name
The parameter must be an instanceof the given class or interface name. PHP 5.0.0 -
self
The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods. PHP 5.0.0 array
The parameter must be an array. PHP 5.1.0-
callable
The parameter must be a valid callable. PHP 5.4.0 -
bool
The parameter must be a boolean value. PHP 7.0.0 -
float
The parameter must be a floating point number. PHP 7.0.0 int
The parameter must be an integer. PHP 7.0.0string
The parameter must be a string. PHP 7.0.0-
iterable
The parameter must be either an array or an instanceof Traversable. PHP 7.1.0
Solution 4
You can do something like this which has always worked for me
for string
function setData($Name=""){ }
this forces the name to be a string, it doesn't check if its a string
for numeric values
function setData($age=0){ }
this forces the age to be a number, if a string is passed, the value will be 0
for array values , there are two variation
function setData(array $data){ }
if an array is not passed, it would throw an error
function setData($data=array()){ }
This would pass an empty array of no value is given for $data
Solution 5
string
, int
and other built-in types are not classes, in argument you specify class, of the argument. The only supported built-in type to be put there is array
.
oscurodrago
Updated on July 09, 2022Comments
-
oscurodrago almost 2 years
I'm trying to make a function with declared argument types, to quickly check if they are in the right format, but when it returns a string I this error:
Catchable fatal error: Argument 2 passed to myfunction() must be an instance of string, string given, called in path_to_file on line 69 and defined in path_to_file on line 49
Example
function myfunction( array $ARRAY, string $STRING, int $INTEGER ) { return "Args format correct"; } myfunction(array("1",'2','3','4'), "test" , 1234);
Where is the mistake?
-
Gumbo over 12 yearsIt does know the types, it just doesn’t care.
-
zerobandwidth almost 9 yearsSpecifying default values for arguments is an important technique but shouldn't be equated with type hinting. For example, it's not entirely true that specifying
func( $x=0 )
forces the value of$x
to be a number. Rather, if your function always processes the value of$x
as a number throughout, then by coincidence it works out. Actually if you pass in a string that happens to be parsable as a number, then it will be processed that way.func( '5' )
would cheerfully use 5 as the integer value. A string that isn't parsable is interpreted as zero, so your 0 default works by coincidence. -
phil294 about 8 yearsthis answer is outdated, see php.net/manual/en/…
-
ankit suthar almost 7 yearsPlease add more description regarding your answer.
-
Roger Campanera over 5 yearsYou can use array as hint since PHP 5.1.0
-
Machado over 5 yearsOf course, you're right, I wrote it right myself after writing wrong hehe.
-
Alex Lacayo about 5 yearsis it always recommended to include parameter type?
-
a-ctor about 5 years
boolean
as type declaration will check forinstanceof boolean
instead of checking for a boolean value. The correct type declaration isbool
. e.g.function a(value: boolean); a(true)
will fail. -
johannchopin over 4 yearsWhat about the syntax of the return type of this function?
-
arm about 4 yearsWhat if I have a class
Person
and I want to accept an array ofPerson
as parameter for my function? How can I do that? -
Daniel Wu almost 4 years@AlexLacayo I think it's good practice. PHP has always been very lenient when it comes to variable type and assigment. But being too open will inevitably lead to head scratching bug one day. Return type is declared using a colon, followed by return type. For example : function foo() : int { return 0; }
-
mehov almost 3 yearsShouldn't it be
bool
instead ofboolean
according to php.net/manual/en/language.types.declarations.php? -
Amal Ajith over 2 years@mehov yes, type support have become more robust in PHP since I answered it in 2017 when the documentation had boolean instead of bool. I will update the answer so it helps others who see it now.