Type hinting

To ensure only the correct data-type objects (i.e. of the correct class) are used when passed into a method, a technique referred to as type hinting can be used. If the wrong type of object is then passed into that method a fatal error will result, as demonstrated in the following example:

	class Bike {
		public function display(){
			echo "Bikes are fun!<br>";

	class Car {}

	function output(Bike $aprilia){

	$mille = new Car;

Save & refresh browser:

Catchable fatal error: Argument 1 passed to output() must be an instance of Bike, instance of Car given, called in /root/user/public_html/php/index.php on line 15 and defined in /root/user/public_html/php/index.php on line 10


Since the $mille object is of the Car class type, it causes an error when the output() function is used, which expects a Bike class type, as can be seen on line 10 above.


The use of class type hints overcomes the need for separate instanceof functions, and are simple way to simplify and resolve bugs.

Leave a Reply