Back to Top

What is Type Hinting in PHP5

What is Type Hinting in PHP5

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

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:

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)

  1. […] the last article I have explained about one of the OOPs concept is Type hinting and today i am going to explain one more OOPs concept and its static keywords in newer PHP that is […]

    1. Wow, that’s a ralely clever way of thinking about it!

  2. […] the last article I have explained about one of the OOPs concept is Type hinting and today i am going to explain one more OOPs concept and its static keywords in newer PHP that is […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use JavaScript Promise API?

Posted on 7 years ago

Bhumi

To learn MySQL PACK_KEYS with example

Posted on 9 years ago

Bhumi

Getting started with Laravel

Posted on 7 years ago

Bhumi