Understanding Callbacks, Anonymous Functions, and Closures in PHP
This article explains the concepts of callbacks, anonymous functions, and closures in PHP, providing definitions, practical examples, and demonstrating how to implement sorting with callback functions, create inline anonymous functions, and use closures to capture external variables, highlighting their benefits for code reuse and readability.
For a long time I was confused about callbacks, anonymous functions, and closures, so I wrote this article to clarify these three terms, starting with their definitions.
1. Callback Functions
A callback function is a function that can be passed as an argument to another function and is invoked at a specific time or event. Example in PHP:
<code>function sortNumbers(array $numbers, callable $sortingFunction): array
{
for ($i = 0; $i < count($numbers) - 1; $i++) {
for ($j = $i + 1; $j < count($numbers); $j++) {
if ($sortingFunction($numbers[$i], $numbers[$j])) {
$temp = $numbers[$i];
$numbers[$i] = $numbers[$j];
$numbers[$j] = $temp;
}
}
}
return $numbers;
}
</code>The function sortNumbers accepts an array of numbers and a callback that defines the sorting logic. Using nested loops it compares elements. Example usage for ascending order:
<code>$unsortedNumbers = [5, 3, 1, 8, 2];
$sortedAscending = sortNumbers($unsortedNumbers, function($a, $b) {
return $a > $b;
});
print_r($sortedAscending); // Output: [1,2,3,5,8]
</code>Another example shows calling sortNumbers with named callbacks 'ascending' and 'descending' to sort in both directions.
2. Anonymous Functions
An anonymous function has no name and is defined inline, often stored in a variable for one‑time use. Example:
<code>$addNumbers = function(int $a, int $b) {
return $a + $b;
};
echo $addNumbers(5, 3); // Output: 8
</code>Anonymous functions improve reusability, readability, and decouple code. PHP 7.4 introduced arrow functions, a more concise syntax:
<code>fn(int $a, int $b) => $a + $b;
</code>Example using an arrow function:
<code>$addNumbers = fn(int $a, int $b) => $a + $b;
echo $addNumbers(5, 3); // Output: 8
</code>3. Closures
A closure is an anonymous function that captures variables from its surrounding scope. Example of a square calculator:
<code>function generateSquareCalculator(){
$square = function($number) {
return $number * $number;
};
return $square;
}
$squareCalculator = generateSquareCalculator();
echo $squareCalculator(5); // Output: 25
</code>Another example creates a multiplier that captures a factor:
<code>function generateMultiplier($factor){
return function($number) use ($factor){
return $number * $factor;
};
}
$multiplierByFive = generateMultiplier(5);
echo $multiplierByFive(10); // Output: 50
</code>In summary, callbacks, anonymous functions, and closures are three important function types in PHP, each with distinct characteristics and use cases. Understanding them can significantly improve PHP programming skills.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.