Type hinting in PHP with example in detail

Spread the love

What is type hinting in PHP?

As the name suggests, Type hinting in PHP is a hint about the data type of an argument in the function definition.

Type hinting provides better data handling used in a program, as type hinting will strictly check the data send to the functions in a program.

Currently, we are having two famous versions of PHP, PHP version 5 and PHP 7.

In PHP 5 you can use type hinting for arrays and objects(using a Class name and Interface name) while in PHP 7 you can use to check scalar types(int, float, string, bool) also with arrays and objects.

Type hinting in PHP in Array

If you want that the function should only accept the array in its argument then you can do this by adding a word “array” in before the argument variable in a function definition.


Example:

function showColors(array $colors)
{

}



Let’s see the simple program.

<?php
 function showColors(array $colors)
 { 
  echo "List of colors=>";
  foreach($colors as $col)
  {
   echo "<br>".$col;
  }
 }
 
 showColors('Red'); //A string 'Red' is sent instead of an array.
?>

If you run above program you will get a fatal error as – “Fatal error: Uncaught TypeError: Argument 1 passed to showColors() must be of the type array, string given, “.

As you in the function call “showColors(‘Red’);” we have sent a simple string ‘Red’ instead of an array.

Hence when function showColors receives a string, due to array type checking it generates a Fatal error indicating argument type must be of an array type.

Now let’s pass an array to the same function showColors.

<?php
 function showColors(array $colors)
 { 
  echo "List of colors=>";
  foreach($colors as $col)
  {
   echo "<br>".$col;
  }
 }
 
 $colors = ['Red','Green','Blue','Yellow'];
 showColors($colors);
?>



The output generated will be

List of colors=>
Red
Green
Blue
Yellow

Now after passing an array of a string function is working properly as the type of function argument which is array is satisfying.

Type hinting in PHP in Object

Using type hinting you can also force a function to accept only an object. For object type checking in a function definition, we have to put a class name in front of an argument.


Let’s see an example.

<?php 
 class Calculator
 {
  public function add($a,$b)
  {
   return $a + $b;
  }
 }
 
 
 class Test
 {
  public function __construct(Calculator $sum)//Object type hinting
  {
   echo "Sum of 2 and 3 = ".$sum->add(2,3);
  }
 }
 $calc = new Calculator();
 $test = new Test($calc);
?>

As you see in the above program in a constructor of a Test class, we are checking whether the argument passed is an object of a class “Calculator” or not.

Scalar type hinting in PHP 7

In PHP version 5 scalar type hinting i.e. for data types integer, float, string and boolean is not available.

But in PHP 7 scalar type hinting is available for basic data types.


Let’s see an example.

<?php
 class Employee
 {
  private $empNo;
  private $salary;
  private $name;
  private $isFullTime;
  
  public function setEmpNo(int $num)
  {
   $this->empNo = $num;
  }
  
  public function setSalary(float $sal)
  {
   $this->salary = $sal;
  }
  
  public function setName(string $name)
  {
   $this->name = $name;
  }
  
  public function setIsFullTime(bool $isfull)
  {
   $this->isFullTime = $isfull;
  }
 }
 
 $emp = new Employee();
 $emp->setEmpNo(123);
 $emp->setSalary(89000.50);
 $emp->setName('Jhon');
 $emp->setIsFullTime(true);
?>

In this simple way, you are able to check the data type of passed arguments to the function using type hinting.

Thus Type hinting in PHP moves PHP towards becoming the more robust language.

In the next chapter, we will see how can we do type hinting for abstract class and interface which will help in the dependency injection design pattern.


Spread the love

Leave a Comment