Type hinting

844 0

Type hinting forces our functions and methods to get only arguments that belong to a specific class, a specific interface, or to arrays. This practice can be most advantageous because it results in better code organization and improved error messages.
This tutorial explains type hinting for arrays and objects, while type hinting for interfaces will be covered in the next one.
How to do array type hinting?                                                                      
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:

In the following example, the calcNumMilesOnFullTank() function calculates the number of miles a car can be driven on a full tank of gas by using the tank volume as well as the number of miles per gallon (mpg). This function accepts only array as an argument, as we can see from the fact that the argument name is preceded by the array keyword.

First, let’s try to pass to the function an argument which is not an array to see what might happen in such a case:

Result:                                                                                             Catchable fatal error: Argument 1 passed to calcNumMilesOnFullTank() must be of the type array, string given    This error is a precise description of what went wrong with our code. From it, we can understand that the function expected an array variable, and not a string. Let’s rewrite the code and pass to the function an array with the expected items, including the model names, the tank volumes, and the mpg (miles per gallon).

Result:
Toyota : 528
BMW : 533
Now it’s working because we passed to the function the array that it expected to get.
How to do object type hinting?
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.

Does PHP support type hinting to basic data types?                                                        
It depends. Whereas PHP5 doesn’t allow type hinting for basic data types (integers, floats, strings and booleans), PHP7 does support scalar type hinting.
PHP5 does not support type hinting to basic data types like integers, booleans or strings. So, when we would like to ensure that an argument belongs to a basic data type, we can use one of PHP’s “is_” family functions. For example:

  •   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.
The following code example can only work in PHP7.

Comments

comments

In this article

Join the Conversation