Back to Top

Type Hinting Callable Functions in PHP

Type Hinting Callable Functions in PHP

PHP supports Type Hinting concept from the version PHP 5 and I have explained about Type Hinting before but today I am going to explain one more concept about Type Hinting which is Type Hinting Callable Functions in PHP. Let’s take a look at this feature and show it in action.

What is Type Hinting in PHP?

Type Hinting in PHP is enforcing the users to must pass type of the instance or parameter while calling the function. Type hinting is mainly useful to prevent the user from passing the incompatible values and creating a standardized code if you are working in a team.You can use Type hinting to specify the data type of an argument in a function declaration.

I have written all about Type Hinting in PHP in my post. You can read here. What is Type Hinting in PHP5

If you are going to enforces function with type hinting, it will check the function whether or not the arguments are of the specified type while calling the function. If not, It will generate runtime fatal error and execution will be halted.

Example

Here is the basic Example of Type Hinting:

This code will give you a below error in PHP version < 7. As string data type is passed over here in function argument. Catchable fatal error: Argument 1 passed to typeHintingExample() must be an instance of string, string was given, called in file.php on line 7 and defined in file.php on line 2

But this code will work in PHP 7 because you can see that, in above code type hinting is defined as a string and an argument is passed as string but instance of string is required to pass because scalar data types like strings or integer data types are not supported in Type hinting if PHP version < 7.

What is callable?

A Callable is a pseudo type data in PHP which is specifically to say that something can be called as a function. You can define the value of pseudotypes in many different ways and use a function. In layman language, Callable itself says it is callable means you can call anything using that callable option.

Callable type-hinting has been introduced in PHP 5.4. The $callable() is faster and readable syntax and also it can be used to invoke a function referenced by the $callable variable.

What is Type Hinting Callable Functions in PHP?

You can type hint the PHP methods parameters with the callable keywords from PHP version 5.4. Callable keywords allow you to pass the data type of parameters and providing hints to function to only accept the given data type.

In PHP 5.4, You will get the Callable Type hint functionality. Type Hinting Callable Functions in PHP allows you to check whether a thing is callable as a closure or an invokable object or some other valid callback.

The Type Hinting is therefore, a very powerful and flexible tool that allows you to determine additional rules within the OOP applications.

You can confirm the variable value by using either is_callable() or the callable type declaration. is_callable () function checks whether it is callable or not if callable, returns true.

NOTE: Callable functions registered with such features as call_user_func () and
call_user_func_array () so It will not be called if it doesn’t catch the exception thrown in a previous callback-function.

How to use Callable Type Hinting in PHP?

Here is a basic example of callable Type Hint in PHP.

Here, I have created a class named TypeHintCallableClass which contain a function TypeHintCallableMethod. One callable function callTypeHintCallable which have $callable. so if I will pass class object and method parameter while calling the callTypeHintCallable function, it will callback the class and now you would see the message Hi, TheCreativeDev.

This is a pretty neat trick to write the callable type hint code and use it only when you need it.Notice that first parameter callable $callable which means expects something to be callable function or class. This way you can use the type declaration.

Also, You can write code like below. Here in this below code,$callable() changed to call_user_func($callable); because call_user_func() function work same as $callable() so you can use callback function and callable interchangeably depends on your requirement.call_user_func() does not support passing non-objects by reference, so you can use call_user_func_array().

$callable() might give a fatal error if given a string referencing a static method.You can work-around the problem by using an array to reference the static method like [‘ClassName’, ‘methodName’].

You can not call the callable function directly using callTypeHintCallable( TypeHintCallableClass::TypeHintCallableMethod), You need to pass it like [‘ClassName’, ‘methodName’]

The callable Type Hint is_callable() will trigger an autoload of the class if the value is a static method callable.

You can use $callable() syntax in each and every dynamic function call from the PHP 7. There is no need to use call_user_func() or call_user_func_array() anymore. As the $callable() syntax supports kind of design ‘ClassName:: method’ in dynamic callbacks from the version PHP 7 which can be fast and support all methods for defining a callable.

Thanks to it you can have more control over the parameters of functions and methods, making more robust scripts.

There you have it!

Have you found an interesting way to use the Type Hinting Callable Functions in PHP? If yes, let us know your views or suggestion on this. Share your thoughts in the comments below!

Like this post? Share it with your friends!

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

What are Lambdas and Closures in PHP?

Posted on 7 years ago

Bhumi

How to use jQuery Lazy Load Plugin?

Posted on 10 years ago

Bhumi