Type Hinting

  • In simple word, 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.

Example 1

<?php  
      
    class a  
    {  
        public $c= "hello javatpoint";  
    }  
    //create function with class name argument  
    function display(a $a1)  
    {  
        //call variable  
        echo $a1->c;  
    }  
    display(new a());  
?>  

Output:

hello protutorials

Example 2

<?php  
      
    class Test1  
    {  
        public $var= "hello protutorials and tech";  
    }  
    //create function with class name argument  
    function typehint(Test1 $t1)  
    {  
        //call variable  
        echo $t1->var;  
    }  
    //call function with call name as a argument  
    typehint(new Test1());  
?>  

Output:

hello protutorials and tech

Leave a comment