PHP Type Hinting

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.

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 another 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, 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


Example: PHP Type Hinting

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.

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 at the following code.


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","");
$objController = new Controller("Facebook","");
$objIntegration = new Integration("jQuery","1.8");

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.

Integrated Software: jQuery 1.8

Download PHP Type Hinting Source Code

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

↑ Back to Top