In PHP, the callback functions are related to the dynamic behavior and flexibility in code execution. They are used to pass custom logic or functions as arguments to other functions, letting developers change how a function behaves without changing its main structure. This makes the code more reusable, modular, and adaptable to different situations.
In this article, we will discuss PHP callback functions, their types, and how they are used.
What is a Callback Function in PHP?
A callback function is simply a function that you can pass as an argument to another function and invoke at a later time. In PHP, callbacks are most commonly used when you need to allow dynamic behavior for functions, allowing users or other functions to determine what specific code should run.
A callback function can be:
- A function name as a string.
- An array containing a class and method, typically used for object-oriented programming.
- An anonymous function (closure) that is defined inline and passed as an argument.
Now, let us understand with the help of the example:
<?php
function greet($name) {
return "Hello, " . $name . "!";
}
function processUser($callback) {
$name = "GFG";
echo $callback($name);
}
processUser('greet');
?>
Output
Hello, GFG!
In this example:
- The processUser() function accepts a callback function as a parameter.
- The callback function, greet(), is passed as a string 'greet' to processUser().
- Inside processUser(), the callback function is invoked with the argument John, which results in the output: Hello, John!.
Types of Callback Functions
Below are the following types of Callback functions in PHP.
1. Simple Callback Function
In the simplest form, you can pass the name of a function as a string to another function, and that function will be called when the callback is invoked.
<?php
function square($number) {
return $number * $number;
}
function applyCallback($numbers, $callback) {
foreach ($numbers as $number) {
echo $callback($number) . "\n";
}
}
$numbers = [1, 2, 3, 4, 5];
applyCallback($numbers, 'square');
?>
Output
1 4 9 16 25
In this example:
- The square() function calculates the square of a given number.
- The applyCallback() function accepts an array of numbers and a callback function. It then applies the callback function to each number and prints the result.
- The callback 'square' is passed to applyCallback(), which processes the numbers in the array by squaring each of them.
2. Object Method Callback
In object-oriented PHP, you can pass the method of an object as a callback by using an array format. This allows you to dynamically choose which method to call.
<?php
class Greeter {
public function greet($name) {
return "Hello, " . $name . "!";
}
}
function processUserMethod($callback) {
$name = "GFG";
echo $callback($name);
}
$greeter = new Greeter();
processUserMethod([$greeter, 'greet']);
?>
Output
Hello, GFG!
In this example:
- Here, [$greeter, 'greet'] is passed as a callback, where $greeter is an instance of the Greeter class and greet is the method.
- The greet() method is called dynamically inside processUserMethod().
3. Anonymous Function Callback
An anonymous function can be used as a callback in situations where you need to define a function on the fly, without declaring it beforehand.
<?php
function processUserAnonymous($callback) {
$name = "GFG";
echo $callback($name);
}
processUserAnonymous(function($name