PHP › Closures & Anonymous Functions
How do first class callable syntax and higher order functions in modern PHP support building flexible, composable, functional style code?
AdvancedAnswer
PHP 8.1 introduced first class callable syntax, letting you reference an existing named function or method as a closure using a concise syntax, and combined with higher order functions, meaning functions that accept or return other functions, PHP supports a genuinely functional programming style where you can compose small, reusable pieces of behavior together, such as chaining several transformation functions over a collection of data, without needing to write a dedicated class for each individual operation.
class Calculator {
public function double($n) { return $n * 2; }
}
$calc = new Calculator();
$doubleFn = $calc->double(...);
$result = array_map($doubleFn, [1, 2, 3]);Real-world example A data processing pipeline composes several small, independently testable functions together using first class callable syntax, building a complex transformation out of simple, reusable building blocks rather than one large, monolithic function.