Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

PHP first-class callable syntax

Reference for PHP's first-class callable syntax (PHP 8.1): turn functions and methods into Closure objects with strlen(...), $obj->method(...) and Class::method(...).

The first-class callable syntax creates a Closure from an existing function or method by calling it with ... in place of the argument list. It was added in PHP 8.1. Writing strlen(...) does not call strlen; it gives you an object you can call later or pass around.

It replaces older, error-prone ways of referring to callables: strings like 'strlen', arrays like [$obj, 'method'] and Closure::fromCallable(). Because it is real syntax, editors and static analyzers can check the name.

PHP
$length = strlen(...);
echo $length("hello");        // 5
$upper = $formatter->upper(...);

Try it

PHP Functions, methods and static methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Callable expressions and the old way

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
$closure = functionName(...);
$closure = $object->method(...);
$closure = Class::staticMethod(...);
$closure = $callableValue(...);
$closure = new Foo(...);   // not allowed
Form Result
name(...) A Closure for a global or namespaced function.
$obj->method(...) A Closure bound to $obj, respecting visibility from the place where it is created.
Class::method(...) A Closure for a static method.
$var(...) A Closure from any callable held in a variable, including invokable objects.

Return value: a Closure object with the same signature, parameters and return type as the target.

Errors: an undefined function or method throws Error when the expression is evaluated. new Foo(...) is a compile error, and the nullsafe form $obj?->method(...) is not allowed.

Usage notes

  • Visibility is captured. A private method can be turned into a closure inside the class and handed to outside code, which can then call it (the asCallback() example).
  • It is not a call. strlen(...) costs no function call; arguments are not evaluated.
  • Same as Closure::fromCallable(), but shorter and checked at compile time for the syntax. Magic __call and __callStatic targets also work.
  • Better than strings and arrays. 'double' or [$obj, 'm'] are not found by "rename" tools or static analysis; double(...) is.
  • Good with array functions. array_map(trim(...), $items) is short and clear. Be careful with functions that take optional extra parameters, because array_map and friends may pass more arguments than you expect (for example array_filter with ARRAY_FILTER_USE_BOTH).
  • Keeps the callee's signature. Named arguments work on the resulting closure, because it has the original parameter names.
  • Not an arrow function. It does not capture variables; it just refers to something that already exists. For custom logic use fn.
  • Static analyzers know the type Closure(string): int, giving better checking than callable strings.