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.

Share:
Share on Facebook1

Share on LinkedIn0Tweet about this on TwitterShare on Google+1Buffer this pageDigg thisShare on Reddit0

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 *

Most Popular Posts

How to Define and Call Case functions in PHP

Posted on 8 months ago

Bhumi

What is Wpautop() function in WordPress

Posted on 2 years ago

Bhumi

The Readers’ Poll – October 2012

Posted on 4 years ago

Bhumi

How to use Short Hand Array in PHP

Posted on 2 years ago

Bhumi

HTML5 Geolocation API with Example

Updated 17 years ago

Bhumi

Overview of Views in MySQL

Updated 17 years ago

Bhumi

What is Closure in JavaScript?

Updated 17 years ago

Bhumi

How to use JavaScript Promise API?

Updated 17 years ago

Bhumi

Type Hinting Callable Functions in PHP

Updated 17 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 1 year ago

Bhumi

The Reader’s Poll – August 2014

Posted on 2 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 2 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 3 years ago

Bhumi