Recently I have prepared some OOPs demos for my students and I really like one concept of Type Hinting so let’s have a quick look at What is Type Hinting in PHP5. Type Hinting in PHP is a new concept of OOPs in PHP and it is included in PHP5.Type Hinting is one kind of function which force to arguments to be an Object or an array.So from now,you can specify the object type as the input parameter to a function.
What is Type Hinting in PHP?
PHP is not a strictly typed language means variable type declaration is not needed at all in PHP.Type declaration increases the readability of the code. So Now, you are able to force parameters to be objects.When you define a function, you can specify the type of its parameter as an object.
Type Hinting in PHP is a mechanism to allow developers to specify the type of function arguments and return.From PHP5,You can add type for function and method parameters which can be used as a Type Hint.
The definition of the Type Hinting specifies the parameter type when you call the function.If the argument type and the specified type does not match, PHP will generate a fatal error.In short, It allows you to control the execution how the type of data passed to the function.
Basic Example of Type Hinting in PHP
Let’s see some basic and common example which shows you type hinting with function, with array and Object, With Allow NULL
NOTE: Type Hinting in PHP is not supported with int and string data types
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Type_hint{ function hint_method(array $arr){ print_r($arr); } } class Type_hint_new { function hint_object(Type_hint $obj){ // Here, If I didn’t pass in an Type_hint object to hint_object(), a FATAL_ERROR was occurred. //echo $obj->hint_method(); // Fatal Error: Argument must be an array echo $obj->hint_method(array('P','H','P')); //First parameter must be an object of Type_hint class } function hint_null($obj = NULL) { echo "Allow NULL"; } } $obj = new type_hint(); $obj_new = new Type_hint_new(); $obj->hint_method(array('b','h','u','m','i')); $obj_new->hint_object($obj); $obj_new->hint_null(NULL); |
Here in above code, I have password array into hint_method and returned value accepts function parameter.When the function is called, PHP automatically checks object before the function’s code starts executing. If it fails, it will fail with an error.So make sure you are passing object which can handle it.
Now lets check the type hint results:
1 2 3 4 5 | Array ( [0] => b [1] => h [2] => u [3] => m [4] => i ) Array ( [0] => P [1] => H [2] => P ) Allow NULL |
NOTE: To check errors of type-hints,new error level E_RECOVERABLE_ERROR is implemented from PHP5.
Type hinting works with interfaces and abstract class too. You also like my posts How to use Namespace in PHP and Constructor and Destructor in PHP.Now it’s your turn: what do you think type hint is useful or not? Finally, suggest you to write in the comments where you have used type hint in PHP for your application.
Comments (3)