How to Use Callbacks in PHP

A callback is considered a function reference/object with the type callable.

It can act as an object method, a function, or a static class method.

Here, we will demonstrate the ways of using standard callbacks, static class method callbacks, object method callbacks, as well as closure callbacks.

Using Standard Callbacks

In PHP, you can call functions with the help of the call_user_func() function. The argument is the string name of the function that is going to be called.

Here is an example:

<?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');

?>
W3docs

Using Static Class Method Callback

In PHP, you can call static methods with the help of call_user_func(). Here, the argument is an array that includes the class string name and the method within it to be called.

<?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(['Article', 'parent::someFunction']);

?>
W3docs Article
W3docs Article
Parent W3docs

Using Object Method Callback

In PHP, you can call object methods with the help of call_user_func(). 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

// PHP program to demonstrate the working
// of a object method callback

// Sample class
class GFG
{
  // Function to print a string
  static 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);

?>
W3docs
invoke W3docs

Closure 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 array_map(). 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

// 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);

?>
Geeksforgeeks
GFG
Article

Using anonymous functions

<?php

// Array of strings
$string_array = ["Geeksforgeeks", "GFG", "Article"];

// Callable closure
array_map(function($string) {
    echo $string . "\n";
}, $string_array);

?>
Geeksforgeeks
GFG
Article