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

snippet
<?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:

Type Hinting

Example 2

snippet
<?php
	
	class Test1
	{
		public $var= "hello javatpoint and SSSIT";
	}
	//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:

Type Hinting
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents