Type hinting in php 5
Type hinting in php 5 and 7 can be most advantageous because it results in better code organization and improved error messages.
When we would like to force a function to get only arguments of the type array, we can put the keyword array in front of the argument name, with the following syntax:
if you define the input parameter as an array and pass something else while function calls it to show fatal error.
You may also like: OOPS Interview Questions and Answers
Type hinting can also be used to force a function to get an argument of type Object. For this purpose, we put the name of the class in front of the argument name in the function.
In the following example, the class's constructor can only get objects that were created from the Driver class. We ensure this by putting the word Driver in front of the argument name in the constructor.
It depends on php version.
PHP 5 does not support type hinting to basic data types like integers, floats, Boolean or strings. So, when we need to validate that an argument belongs to a basic data type, we can use one of PHP's "is_" family functions.is_bool - to find out whether a variable is a boolean (true or false).
is_int - to find out whether a variable is an integer.
is_float - to find out whether a variable is a float (3.14, 1.2e3 or 3E-10).
is_null - to find out whether a variable is null.
is_string - to find out whether a variable is a string.
On the other hand, PHP7 does support scalar type hinting. The supported types are integers, floats, strings, and booleans.You may also like: Core PHP Interview Questions Answers