Posted by: phillipnb | July 9, 2012

Type Hint in PHP


Type Hinting in PHP is quite new. This feature was introduced into PHP from version 5.1.What Type Hint does is that it forces function (or method) parameters to be of a specified type. If it is not of a specified type then it will throw an error. Usually, the function parameters in PHP are not forced to be of any particular type.

Example:

function addNumbers($num1,$num2)
{
   echo "Number 1 = ".$num1.','."Number 2 = ".$num2;
}

Ideally, we expect $num1 and $num2 to be numbers. But if we pass a string like this, addNumbers(‘a’,’b’), the function is not going to throw an error. This is not the case when Type Hint is implemented.

Example for Type Hint in PHP

class Apple
{
	public function func1(Grape $objg)
	{
		echo "\n ".$objg->var2;
		echo "\n ".$objg->var3;
	}

	public function func2(Array $array1)
	{
		print_r($array1);
	}

	public function func3(Traversable $iterator)
	{
        echo get_class($iterator);
    }

   	public function func4(callable $callback)
   	{
        call_user_func($callback, $data);
    }

    public function func5()
    {
    	echo "\n Hello from func5";
    }
}

class Grape
{
	public $var1 = '500';
	public $var2 = '250';
	public $var3 = 'Hello';

	public function func1()
	{
	}
}

class Orange
{
	public $var1 = '100';
	public $var2 = '50';
	public $var3 = 'Greetings';

	public function func1()
	{
	}
}

$objgrape = new Grape;
$objorange = new Orange;
$objA = new Apple;
$temp_array = array('a','c','b','d');

$objA->func1($objgrape);

If we run the above piece of code, then we get the output as

250
Hello

Next, let us try this:

$objA->func1('Hello World');

In this example using Apple Class, you can see that the method func1 is expecting an instance of type Grape. So, the above call will result in an error, ” Argument 1 passed to Apple::func1() must be an instance of Grape, string given…”.
Next, let us place this call:

$objA->func1($objorange);

This will throw an error “Argument 1 passed to Apple::func1() must be an instance of Grape, instance of Orange given…” because func1 is expecting an instance of Grape.

Next, we will try this call:

$objA->func1($temp_array);

This call will throw the error “Argument 1 passed to Apple::func1() must be an instance of Grape, array given..”. Let us place a new call to func2 with an array as parameter

$objA->func2($temp_array);

As func2 is expecting an array as parameter, we get the following as output:

Array
(
    [0] => a
    [1] => c
    [2] => b
    [3] => d
)

Thus, by using type hint we can tell the caller of a class method of what type the parameters should be.

Passing Objects as Function Parameters

By now you must be wondering whether it is possible to pass objects as function parameters. Yes, it is possible. The below examples shows that you can pass objects as parameter with or without type hinting.

class A
{
	public function func1(B $objb, A $obja)
	{
		echo "\n Hello from class A func1";
		print_r($objb->var1);
		$obj_c = new C();
		$objb->func2($obj_c);
	}
}

class B
{
    public $var1 = '123';
	function func2(C $objc)
	{
		echo "\n".print_r($objc->var1,true);
	}
}

class C
{
    public $var1 = '888';
	function func2($objx)
	{
		echo "\n from class C, func2, ".$objx->var1;
	}
}
$obj1 = new A();
$obj2 = new B();

$obj1->func1($obj2,$obj1);
$objy = new C();
$objy->func2($objy);

When we make a call like this, $obj1->func1($obj2,$obj1); we make sure that the first parameter is of type class B and the second one is of type class A because the method/function is expecting these parameters to be of specified type. But if we make a call like this, $objy->func2($objy);, we need not worry what we are passing until we start using the parameter that was passed into the class method.

As always, I welcome your valuable comments about type hinting and passing objects as function parameters.

Happy PHPing

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: