PHP 8.1 first-class callable syntax: turning a function name into a Closure

Since PHP 8.1 you can turn any directly-callable expression into a Closure with a tiny new syntax:

$len = strlen(...);   // a Closure, equivalent to Closure::fromCallable('strlen')

No more 'strlen' strings, no more [$obj, 'method'] arrays. It looks like sugar, and it mostly is — but the syntax has sharp edges that silently produce wrong results or late-failing bugs. Here are the six that matter.

What you get vs the old style

Before 8.1, a “callable” was usually a string or an array, resolved only when invoked:

// Old: the engine checks the name at call time
$names = array_map('strlen', ['a', 'bb', 'ccc']);
// New: PHP 8.1 first-class callable syntax
$names = array_map(strlen(...), ['a', 'bb', 'ccc']); // [1, 2, 3]

The win is not just fewer characters — the new form is visible to static analysis and respects the scope where it is written (more on that below).

Gotcha 1: The ... is syntax, not a function call

strlen(...) does not call strlen. It returns a Closure you invoke later:

$fn = strlen(...);
var_dump($fn);        // object(Closure)#1 (0) { }
// strlen was never run
echo $fn('hello');    // 5 — invoked here

If you come from JavaScript, your brain reads ... as a spread operator. It is not. Drop it into a map/filter and you will be surprised that nothing executed yet.

Gotcha 2: Names are checked at compile time

A typo in a string callable fails only when the callable is finally invoked — often deep inside a library:

array_map('strlenn', $x);   // no error here
// ...later, when the callback fires: "Call to undefined function strlenn()"

The first-class syntax fails at compile time:

array_map(strlenn(...), $x); // Compile-time fatal error: function strlenn not found

That single difference catches whole classes of typos before code ever runs.

Gotcha 3: Instance methods bind to ONE object (the array_map trap)

$obj->method(...) captures $obj. The resulting closure calls method() on that specific instance and ignores whatever argument you hand it. Inside array_map this is a quiet disaster:

class User {
    public function __construct(public string $name) {}
    public function name(): string { return $this->name; }
}

$users = [new User('Ada'), new User('Linus'), new User('Grace')];

// BUG: bound to $users[0]; the per-element argument is discarded
$names = array_map($users[0]->name(...), $users);
print_r($names); // ['Ada', 'Ada', 'Ada']  ← not what you wanted

// FIX: let the closure receive the instance and call on it
$names = array_map(static fn (User $u) => $u->name(), $users);
print_r($names); // ['Ada', 'Linus', 'Grace']

Static methods (User::find(...)) are fine to map over — there is no instance to bind. The trap is only for instance methods captured from a concrete object.

Gotcha 4: It captures scope — but you cannot rebind it

Because the callable is built at the point of writing, it can reach private members of its own class:

class Reporter {
    private function secret(): string { return 'classified'; }
    public function leak(): callable {
        return $this->secret(...); // OK — captured inside the class scope
    }
}

$leak = (new Reporter())->leak();
echo $leak(); // 'classified' — works despite secret() being private

The flip side: the scope is fixed, so a library that tries to Closure::bindTo() it (Laravel’s Macroable does this) will throw Cannot rebind scope of closure created from method. In that spot, fall back to the array syntax [$this, 'secret'].

Gotcha 5: These closures do not serialize

A Closure is not serializable. If you cache callbacks or queue jobs that carry a callable, switching to this syntax breaks persistence:

$cache = ['len' => strlen(...) ];
// serialize($cache); // throws: Closure object cannot be serialized

Keep the string/array form (or just store the name) for anything you serialize.

Gotcha 6: new Foo(...) is NOT a thing

It is tempting to expect object construction as a callable:

// Not supported — `new Foo()` is not considered a "call"
// $make = new User(...);  // compile-time error

// Do this instead:
$make = static fn (string $n) => new User($n);
$ada = $make('Ada');

Cheat sheet

| You want | Write it as |
|———-|————|
| Closure from a function | strlen(...) |
| Closure from a static method | User::find(...) |
| Closure from an instance method | $user->name(...) (bound to that instance) |
| Pass to array_map / usort | array_map(strlen(...), $arr) |
| Closure from a dynamic name | Closure::fromCallable($fn) |
| Construct an object per call | static fn (...) => new Foo(...) |

Bottom line

First-class callable syntax is the cleaner default for PHP 8.1+: it is statically checkable, scope-aware, and drops the string/array ceremony. Just remember the ... is a factory not a call, never capture an instance method to array_map over other objects, and avoid it where something needs to rebind or serialize the closure.