PHP 8.1 introduced a new feature, First-Class Callable Syntax, which significantly streamlines code that relies on callables. This tutorial provides an in-depth guide on effectively using this vital feature.
What is First-Class Callable Syntax?
First-Class Callable Syntax is a significant feature in PHP 8.1. It lets you create closure objects from any expression that PHP's grammar can directly call. It includes function names, class methods, static methods, and anonymous functions. This new syntax effectively replaces the older callable syntax that relied on strings and arrays, resolving their limitations and drawbacks.
How to Use First-Class Callable Syntax?
The first-class callable syntax is a new way of creating callables in PHP 8.1. It employs the same syntax as calling a function or method invocation but with an ellipsis (...) after the expression. The ellipsis is integral to the syntax and not a placeholder for parameters.
For instance, to create a callable that calls the strlen
function, you would write:
$callable = strlen(...);
The $callable
variable now holds a closure object that can be invoked like any other function:
echo $callable('hello'); // 5
You can also pass this Closure object as a callback:
$array = ['apple', 'banana', 'cherry'];
$lengths = array_map($callable, $array); // [5, 6, 6]
Examples of various types of callable expressions include:
class Person {
public function sayHello() { echo "Hello"; }
public static function announce() { echo "I am a static method"; }
public function __invoke() { echo "I am invokable"; }
}
$obj = new Person();
$classStr = 'Person';
$methodStr = 'sayHello';
$staticmethodStr = 'announce';
// Create callable from a function name
$f1 = strlen(...);
// Create callable from an invokable object
$f2 = $obj(...);
// Create callable from a class method
$f3 = $obj->sayHello(...);
// Create callable from a class method using variable name
$f4 = $obj->$methodStr(...);
// Create callable from a static method
$f5 = Person::announce(...);
// Create callable from a static method using variable names
$f6 = $classStr::$staticmethodStr(...);
// Create callable from an anonymous function
$f7 = function ($x) { return $x * 2; }(...);
Why Use First-Class Callable Syntax?
This new syntax has distinct advantages:
- Conciseness and Readability: Eliminates the need for extra brackets or quotes.
- Consistency and Intuition: Employs the same syntax for function or method calling, avoiding different formats.
- Reliability and Security: Eliminates the risk of typos or invalid callables causing runtime errors.
- Compatibility and Interoperability: Supports any expression directly callable in PHP grammar.
- Analyzability: Easier to scrutinize or manipulate using static analysis tools and reflection.
- Robust Type Checking: As Closure objects, these callables enable better type checking, bolstering code robustness.
Limitations
However, this syntax comes with its own set of limitations:
- There is no support for object instantiation via the
new
operator. - There is no support for null-safe methods with the null-safe operator (
?->
). - Cannot pass arguments to an attribute constructor via attributes (
#[...]
). - It does not support partial application of parameters, as it will cause a syntax error.
- Closure objects cannot access the original function's or method's scope.
Conclusion
The first-class callable syntax is a powerful new feature in PHP 8.1 that makes it easier to work with callables. Understanding its usage and limitations enables you to write code that is not only more concise but also more reliable and robust.