Explain type hinting in PHP

In PHP, type hinting is used to specify the expected data type (arrays, objects, interface, etc.) for an argument in a function declaration. It was introduced in PHP 5.

Whenever the function is called, PHP checks if the arguments are of a user-preferred type or not. If the argument is not of the specified type, the run time will display an error and the program will not execute.

It is helpful in better code organization and improved error messages.

Example usage:

//sendEmail() function argument $email is type hinted of Email Class. It means to call this function you must have to pass an email object otherwise an error is generated.
<?php
 function sendEmail (Email $email)
 {
 $email->send();
 }
?>