Skip to main content

PHP Type Hinting

While reviewing code sometime I found that people are using is_a() method to validate input parameter of their functions when they need to take object as input.  This implementation is fine if code is going to be executed in php 4. But if you are running your code on PHP 5.x then there is more robust way is available to validate your function input. From version 5 PHP has concept of Type Hinting, which we can use, so that user of the function can only pass what parameter is expected, otherwise fatal error will be returned. Here in our tutorial PHP Type Hinting we will explore implementation of Type Hinting.

What is Type Hinting in PHP

In simple word to remember, type hinting means providing hints to function to only accept the given data type. In technical word we can say that Type Hinting is method by which we can force function to accept the desired data type. In PHP we can use type hinting for Object, Array and callable data type. Callable type hinting is only available from PHP version 5.4.

Below is a basic example of Type Hinting in PHP:

<?php
//Class where we are going to implement typehinting
class Typehinting_Test{
//Implementation Type Hinting in PHP
//Forcing to pass argument as object of class Test1
public function type_hint_method(Test1 $parameter){
$parameter->test_method();
}
}
//Below is class
class Test1{
public function __construct() {
//Do Nothing
}
//Method to call in the type hinting class
public function test_method()
{
echo 'Type Hinting in PHP works';
}
}
$th_test = new Typehinting_Test();
//$th_test->type_hint_method(1221) //Will give fatal error: Catchable fatal error: Argument 1 passed to Typehinting_Test::type_hint_method() must be an instance of Test1, integer given
$t1 = new Test1();
$th_test->type_hint_method($t1); // Print Type Hinting in PHP works
?>

Type Hinting also works with normal function outside of the class. You can force to pass only object of the class which implements the particular interface. Below is the example:

<?php
class Test1{
public $var = 'this is test';
}
function typehint(Test1 $t1){
echo $t1->var;
}
typehint(new Test1()); // Will print this is test
?>

You can also use interface in the type hinting.

<?php
//Let us create small interface
interface test{
public function abc();
}
//Implement the interface in the class
class test1 implements test{
public function __construct() {
;
}
public function abc() {
echo 123;
}
}
//do not implement interface
class test2{
public function __construct() {
;
}
public function abc() {
echo 123;
}
}
//Method where typehinting is by interface.
function typehinttest(test $t){
$t->abc();
}
//Created object of the class
$objTest1 = new Test1();
$objTest2 = new test2();
typehinttest($objTest1);//It will echo 123
typehinttest($objTest2); // throw error

You can also override the method where you have used type hinting.

<?php
class test1{
public function __construct() {
//do nothing;
}
function abc(){
echo 'typehinging works';
}
}
class parent_class{
function typeTest(test1 $objTest){
$objTest->abc();
}
}
class child_class extends parent_class{
function typeTest($at) {
echo $at;
}
}
$a = new child_class();
$a->typeTest('1233');

You can download tutorial code from below URL https://www.techflirt.com/typehinting.zip

For some interesting things about Type Hinting you can go through comment on the PHP type hinting manual i.e.

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

Leave a comment/Ask Question

One thought on “PHP Type Hinting

  1. I’ve completed your PHP OOP tutorials. I’m very grateful for your job.
    You did one mistake in this tutorial. It is not possible to override the parental method with different type hinting.
    When I tried I had an error:
    Strict Standards: Declaration of child_class::typeTest($objTest) should be compatible with parent_class::typeTest(test1 $objTest)) in C:\xampp\htdocs\type-hinting-oop.php on line 35

shares