PHP Type Hinting

As PHP is a loosely typed language, there is no need to specify data type for variable declaration, function declaration or any where. But, using PHP5, we can specify data type that are passed as arguments of PHP functions. This capability is called as type hinting.

Advantages of PHP Type Hinting

  • Using PHP type hinting, we can know the expected argument type of the function at the time of maintenance.
  • Function declaration and usage would be obvious once the code is handover to other developer.
  • Type hinting in PHP makes the code useful for some future reference.

Possible Types for 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, $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 for 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

php_type_hinting

Example: PHP Type Hinting

We are having two classes named as Controller and Integration. The Controller class holds properties of menu item and the Integration class holds that of integrated software. Both are shown below.

class Controller {
public $menuTitle = "";
public $menuLink = "";
function Controller($menuTitle,$menuLink) {
$this->menuTitle = $menuTitle;
$this->menuLink = $menuLink;
}
}
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 into the following code.

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","http://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 expect this particular object hinted by its class name.

For example, printIntegration() function, will work only on receiving 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

Output

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.

PHPPOT
Facebook
Integrated Software: jQuery 1.8

Download PHP Type Hinting Source Code

This PHP code tutorial was published on May 12, 2013.

↑ Back to Top