I would like or I will prefer indicate the real type of a parameter in a function with Php, it’s possible ?
Example :
<?php
function test($param)
{
echo $param;
}
$str = "hiiiii everyone";
test($str);
?>
Instead of put just $param in the function test; I would like put something like that
function test(string $param) // string is the type of the param, that can be int, long ect or an object which I had created.
It’s possible to do that with the last version of PHP ?
What you are looking for is Type Hinting. However, as stated in the linked manual page:
This means that no, you cannot force the argument for a function to be string. You can force it to be an instance of a specific class, something which is callable, or an array.
If you want to force an argument to be a string, do something like this: