How to Use Callbacks in PHP
Here, we will demonstrate how to use standard callbacks, static class method callbacks, object method callbacks, as well as closure callbacks in PHP.
In PHP, a callback is a callable type that can reference a standard function, a static class method, an object method, or a closure. This tutorial demonstrates how to use standard callbacks, static class method callbacks, object method callbacks, and closure callbacks.
Using Standard Callbacks
In PHP, you can call functions with the help of the <kbd class="highlighted">call_user_func()</kbd> function. The argument is the string name of the function that is going to be called.
Here is an example:
php use standard callbacks
<?php
// PHP program to demonstrate work
// of a standard callback
// Function for printing a string
function someFunction()
{
echo "W3docs \n";
}
// Standard callback
call_user_func('someFunction');
?>php use standard callbacks, output
W3docsUsing Static Class Method Callback
In PHP, you can call static methods with the help of <kbd class="highlighted">call_user_func()</kbd>. Here, the argument is an array that includes the class string name and the method within it to be called.
php use static class method callback
<?php
// PHP program to demonstrate the working
// of a Static class method callback
// Sample class
class GFG
{
// Function used for printing a string
static function someFunction()
{
echo "Parent W3docs \n";
}
}
class Article extends GFG
{
// Function for printing a string
static function someFunction()
{
echo "W3docs Article \n";
}
}
// Static class method callback
call_user_func(['Article', 'someFunction']);
call_user_func('Article::someFunction');
// Relative Static class method callback
call_user_func(['parent', 'someFunction']);
?>static class method callback, output
W3docs Article
W3docs Article
Parent W3docsUsing Object Method Callback
In PHP, you can call object methods with the help of <kbd class="highlighted">call_user_func()</kbd>. Here the argument is an array that includes the object variable and the method string name to be called.
Let’s check out an example:
php use object method callback
<?php
// PHP program to demonstrate the working
// of a object method callback
// Sample class
class GFG
{
// Function to print a string
function someFunction()
{
echo "W3docs \n";
}
// The __invoke() method is called when a script tries to call an object as a function.
public function __invoke()
{
echo "invoke W3docs \n";
}
}
// Class object
$obj = new GFG();
// Object method call
call_user_func([$obj, 'someFunction']);
// Callable __invoke method object
call_user_func($obj);
?>php using object method callback, output
W3docs
invoke W3docsClosure Callbacks
You can make closure functions callable through standard calls or by mapping the closure function to the array of valid arguments sent to the closure with <kbd class="highlighted">array_map()</kbd>. Here, the closure function and the array of its valid arguments are considered the arguments of the function.
The example will look as follows:
php use closure callbacks
<?php
// PHP program to demonstrate the working
// of a closure callback
// Closure for printing a string
$print_function = function ($string) {
echo $string . "\n";
};
// Array of strings
$string_array = ["Geeksforgeeks", "GFG", "Article"];
// Callable closure
array_map($print_function, $string_array);
?>php using closure callbacks, output
Geeksforgeeks
GFG
ArticleArrow Functions (PHP 7.4+)
Anonymous functions in PHP are closures. You can also use the concise arrow function syntax for simpler inline callbacks:
php arrow function callback
<?php
// Array of strings
$string_array = ["Geeksforgeeks", "GFG", "Article"];
// Arrow function callback
array_map(fn($string) => print($string . "\n"), $string_array);
?>php arrow function callback, output
Geeksforgeeks
GFG
Article