Database

Type Hinting in PHP

With Type Hinting we can force the parameter of the function to be object or an array. Type Hinting was introduced in PHP 5. Also we can allow NULL also along with object or array.

For forcing the parameter to be an object we have to specifying the name of the class in the function prototype.

Note: Type Hinting is not supported with [code]int[/code] and [code]string[/code].

Let’s see how it works:

[cc lang=”php”]
class first_class
{
// We have forced the first parameter of below
// Method to be the object of the class second_class

// If you pass other data type or
// Even object of other class it will
// Generate Fatal Error
public function force_class(second_class $second_class)
{
/*
your code goes here
*/
}

// We have forced the first parameter of below
// method to be an array

// So it will generate fatal error if
// you pass parameter other than an Array

public function force_array(array $passed_array)
{
/*
Your code goes here
*/
}

}

class second_class
{
/*
Your code goes here
*/
}

class third_class
{
/*
Your code goes here
*/
}

$first = new first_class();
$second = new second_class();
$third = new third_class();

// Generate error, as string passed
$first->force_class(“string”);

// Generate error, as array passed
$first->force_class(array(“1″,”2”));

// Generate error
// as object of other class passed
$first->force_class($third);

// This will work
// as object of second class passed
$first->force_class($second);

// Generate Error, as string passed
$first->force_array(“string”);

// This will works
$first->force_array(array(“1″,”2”));

[/cc]

Type Hinting for Functions

We can use Type Hinting for functions which are outside the class. Let’s see how it works.

[cc lang=”php”]

// This function will only accept the
// Object of the first_class class
// as a first parameter.

function my_function(first_class $first)
{
/*
Your code goes here
*/
}

[/cc]

Allow NULL with Type Hinting

Yes, We can allow NULL values with the Type Hinting. So in that case parameter must be as per the defined type or it should be [code]NULL[/code].

[cc lang=”php”]

// First Parameter of this function
// Must be an object of the class first_class
// or NULL
function allow_null(first_class $first = NULL)
{
/*
Your Code Goes Here
*/
}

// This works
$first = new first_class();
allow_null($first);

// This also works
allow_null(NULL);

[/cc]

Type Hinting for Array was introduced in PHP 5.1. Type Hinting is not supported with [code]int[/code] and [code]string[/code].

Shares:
  • Maigret Aurélien
    November 8, 2011 at 11:02 am

    Hi,

    Thank for this article. :)

    Small point :

    fucntion force_array
    =>
    function force_array

    Reply
    • Avinash
      November 8, 2011 at 1:47 pm

      Thanks for noticing. Corrected :)
      I don’t know why but my fingers always type the function as fucntion and later I have to correct it.

      Anyways thanks again

      Reply
  • Guest
    Guest
    November 8, 2011 at 10:45 pm

    No problem.
    I have also this “problem” with others words. :)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *