There are some cases when we want a function not to run before a particular task is completed, but we want to run it right after the job has been completed. This is where the callback function comes into play.
A callback function is a function that is supplied as an argument to another function and ensures that it is only executed when a specific task has been completed.
What is Callback Function in PHP?
We can use the callable type declaration to indicate PHP callbacks functions.
Any existing function may be used as a PHP callback function. Pass a string containing the function’s name as an argument to another function to use it as a callback function.
Passing Callback
There are four types of callbacks in PHP. They are:
Simple callback
In PHP, we can use the call_user_func() function to call functions, where arguments are the function's string name.
Example:
PHP
PHP
<?php
// Function to callback function my_callback_function() { echo 'Hello, world!'; }
// PASS my_callback_function INTO call_user_func()
call_user_func('my_callback_function'); ?>
You can also try this code with Online PHP Compiler
We pass my_callback_function() into call_user_func() as a parameter.
The callback function is called from within the function. call_user_func() will call my_callback_function().
Static class method call
We can call static class methods by passing the class name instead of an object at index 0 or passing 'ClassName::methodName' without creating an object of that class.
Example:
PHP
PHP
<?php
// An example static callback method
class MyClass { static function myCallbackMethod() { echo 'Hello World!'.'<br>'; } }
// Static class method callback call_user_func(array('MyClass', 'myCallbackMethod'));
// Static class method callback call_user_func('MyClass::myCallbackMethod'); ?>
You can also try this code with Online PHP Compiler
We can use the call_user_func() function to call object methods, with the parameter being an array containing the object variable and the string name of the method to call.
Example:
PHP
PHP
<?php // A sample class class MyClass { static function myCallbackMethod() { echo 'Hello World!'; } }
Objects implementing __invoke can be used as callables. Object methods can also be called if the __invoke() function declaration is used to make them invokable. In this situation, the object variable is the argument to the call_user_func() method.
Example:
PHP
PHP
<?php class obj { public function __invoke($name) { echo 'Hello ', $name, "\n"; } }
$classObjVar = new obj(); call_user_func($c, 'PHP!'); ?>
You can also try this code with Online PHP Compiler
We can call closure functions via conventional calls or map the closure function to an array of valid arguments using the array_map() function. The arguments are the closure function and an array of valid arguments.
A callback function in PHP can be a string, array, or anonymous function (closure) that references a function or method to be executed later.
What is the callback function used for?
Callback functions are used for deferred execution, such as event handling, array manipulation functions, or asynchronous processing, allowing code to be more flexible and modular.
What is another name for callback function?
Another name for a callback function is a "call-after" or "hook," indicating it is called after a certain event or condition is met.
What is the difference between callback function and regular function?
A regular function is called directly, whereas a callback function is passed as an argument and invoked by another function at a later time or event.
Conclusion
A callback function is a function that is supplied as an argument to another function and ensures that it is only executed when a specific task has been completed. We have looked at the types of callback functions in PHP and also saw some code examples regarding the same.