Thursday, September 4, 2025
HomeLanguagesImplementing callback in PHP

Implementing callback in PHP

In PHP, callback is a function object/reference with type callable. A callback/callable variable can act as a function, object method and a static class method. There are various ways to implement a callback. Some of them are discussed below:

Standard callback: In PHP, functions can be called using call_user_func() function where arguments is the string name of the function to be called.

Example:




<?php
  
// PHP program to illustrate working
// of a standard callback
  
// Function to print a string
function someFunction() {
    echo "Geeksforneveropen \n";
}
  
// Standard callback
call_user_func('someFunction');
?>


Output:

Geeksforneveropen

Static class method callback: Static class methods can be called by using call_user_func() where argument is an array containing the string name of class and the method inside it to be called.

Example:




<?php
  
// PHP program to illustrate working
// of a Static class method callback
  
// Sample class
class GFG {
  
    // Function used to print a string
    static function someFunction() {
        echo "Parent Geeksforneveropen \n";
    }
}
  
class Article extends GFG {
  
    // Function to print a string
    static function someFunction() {
        echo "Geeksforneveropen Article \n";
    }   
}
  
// Static class method callback
call_user_func(array('Article', 'someFunction'));
  
call_user_func('Article::someFunction');
  
// Relative Static class method callback
call_user_func(array('Article', 'parent::someFunction'));
?>


Output:

Geeksforneveropen Article
Geeksforneveropen Article
Parent Geeksforneveropen

Object method callback: Object methods can be called by using call_user_func() where argument is an array containing the object variable and the string name of method to be called. Object method can also be called if they are made invokable using __invoke() function definition. In this case, argument to the call_user_func() function is the object variable itself.

Example:




<?php
  
// PHP program to illustrate working
// of a object method callback
  
// Sample class
class GFG {
  
    // Function to print a string
    static function someFunction() {
        echo "Geeksforneveropen \n";
    }
  
    // Function used to print a string
    public function __invoke() {
        echo "invoke Geeksforneveropen \n";
    }
}
  
// Class object
$obj = new GFG();
  
// Object method call
call_user_func(array($obj, 'someFunction'));
  
// Callable __invoke method object 
call_user_func($obj);
  
?> 


Output:

Geeksforneveropen
invoke Geeksforneveropen

Closure callback: Closure functions can be made callable by making standard calls or mapping closure function to array of valid arguments given to the closure function using array_map() function where arguments are the closure function and an array of its valid arguments.

Example:




<?php
  
// PHP program to illustrate working
// of a closure callback
  
// Closure to print a string
$print_function = function($string) {
    echo $string."\n";
};
  
// Array of strings
$string_array = array("Geeksforneveropen", "GFG", "Article");
  
// Callable closure
array_map($print_function, $string_array);
  
?>


Output:

Geeksforneveropen
GFG
Article
RELATED ARTICLES

Most Popular

Dominic
32262 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11856 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS