Introduce Type Hinting In PHP5


This article explains type hinting using PHP5. PHP5 introduces type hinting. Functions square measure are currently able to force parameters to be objects by specifying the name of the category within the operate prototype, interfaces, arrays or callback hint type. However, if null values are employed because the default parameter value then it'll be allowed as an associate argument for any later decision. If the category or interface is nominative as a type hint then all its kids or implementations square measure are allowed too. Type hints can't be used with scalar sorts like int or string. Resources and Traits don't seem to be allowed either. In type hinting, a function is able to force a parameter to be an object by the specific name of the class. If you pass the null parameter then it will be allowed as arguments. I will show that in the last example of allowing null values as arguments.

A Valid type class of unit category names for arguments that receive objects and array for those who receive arrays. I will show use of this example.

Type hint example class

In this example I will show a simple type hint.

  1. <?php   
  2. class hint  
  3. {  
  4.     public function demo(newclass $overclass) {  
  5.         echo $overclass->var;  
  6.     }  
  7.     // use first parameter like an array  
  8.     public function demo_array(array $input_array) {  
  9.     echo "<pre>"; print_r($input_array);  
  10.     }  
  11.     //use first parameter must be an iterator  
  12.     public function demo_interface(Traversable $iterator) {  
  13.         echo get_class($iterator);  
  14.     }  
  15.     //use first parameter must be a collable  
  16.     public function demo_callable(callable $callback$demo) {  
  17.         call_user_func($callback$demo);  
  18.     }  
  19. }  
  20. class newclass {  
  21.     public $var = 'this is my hint type';  
  22. }  
  23. ?>
Failure to satisfy the type hint results in a catchable error.
  1.  <?php   
  2. include 'demo.php';  
  3. $hint = new hint;  
  4. $overclass = new newclass;  
  5. //show fatal error must be an object class  
  6. $hint->demo('hello');  
  7. // Fatal Error: arguments one must be an instance of newclass  
  8. $foo = new testClass;  
  9. $hint->demo($foo);  
  10. // Fatal Error: Argument first must not be null  
  11. $hint->demo(null);  
  12. //here show my hint class  
  13. $hint->demo($overclass);  
  14. // here show fatel error use arguments one must be an array  
  15. $hint->demo_array('a string');  
  16. // show array  
  17. $hint->demo_array(array('a''b''c'));  
  18. //show the array object  
  19. $hint->demo_interface(new ArrayObject(array()));  
  20. // show the integer value one  
  21. $hint->demo_callable('demo_dump', 1);  
  22. ?>

catchable fatal error

Type hinting also works with functions as in the following

  1.  <?php   
  2. class hint {  
  3.     public $demo = 'this is my hint class also work with function';  
  4. }  
  5. function hintFunction(hint $foo) {  
  6.     echo $foo->demo;  
  7. }  
  8. $hint = new hint;  
  9. hintFunction($hint);  
  10. ?>

type hint class work with function 

Type hinting allowed null value

This example will show a fatal error.

  1.  <?php   
  2. function demo(testClass $obj = NULL) {  
  3. }  
  4. demo(NULL);  
  5. demo(new testClass);  
  6. ?>

null values return fatal error