As PHP is a loosely typed language, there is no need to specify a data type for variable declaration, function declaration or anywhere. But, using PHP5, we can specify the data type that is passed as arguments of PHP functions. This capability is called as type hinting.
In PHP, type hinting is possible only for some specific type of data. For example, functions argument, which is user defined class instance, can be hinted by the name of the class. For example,
function printMenu(Controller $controller) {
...
}
Now, the $controller is hinted by the name of the class Controller, meaning that, the function printMenu() can accept only Controller class object as its argument.
Other than the instances of class, PHP type hinting is also applicable for arrays, interfaces, and callable functions. Rather, this is not capable of objects like int, float, boolean, string and etc. If we use type hinting for the type int, then the execution will be stopped with the following error.
Catchable fatal error: Argument 1 passed to ... must be an instance of int
We are having two classes named as Controller and Integration. The Controller class holds properties of the menu item and the Integration class holds that of integrated software. Both are shown below.
<?php
class Controller
{
public $menuTitle = "";
public $menuLink = "";
function Controller($menuTitle, $menuLink)
{
$this->menuTitle = $menuTitle;
$this->menuLink = $menuLink;
}
}
?>
<?php
class Integration
{
public $name;
public $version;
function Integration($name, $version)
{
$this->name = $name;
$this->version = $version;
}
}
?>
We should save these classes by their name, like, Controller.php and Integration.php. We need to include these classes into a PHP file where we want to implement PHP type hinting. So, let us have a glance at the following code.
<?php
include_once ("Controller.php");
include_once ("Integration.php");
function printMenu(Controller $controller)
{
echo "<a href='" . $controller->menuLink . "' target='_blank'>" . $controller->menuTitle . "</a></br/>";
}
function printIntegration(Integration $integration)
{
echo "<strong>Integrated Software:</strong> " . $integration->name . " " . $integration->version . "<br/>";
}
$objController = new Controller("PHPPOT", "https://phppot.com");
printMenu($objController);
$objController = new Controller("Facebook", "http://facebook.com");
printMenu($objController);
$objIntegration = new Integration("jQuery", "1.8");
printIntegration($objIntegration);
?>
Here, printMenu() and printIntegration() functions accepts the instances of Controller and Integration classes respectively.
When we create the object by passing the required number of argument, it will be set as the value of class properties. Then, this object will be passed as the argument of the function which expects this particular object hinted by its class name.
For example, printIntegration() function, will work only on receiving an instance of Integration class. If we pass the instance of Controller class, then the following error will occur.
Catchable fatal error: Argument 1 passed to printIntegration() must be an instance of Integration, instance of Controller given
Save the above PHP file as type_hinting.php. After ensuring that the above files are in PHP root directory, run type_hinting.php. Then, the output will be as follows.
<code">PHPPOT Facebook Integrated Software: jQuery 1.8
Download PHP Type Hinting Source Code
Thanks, great explanation.