First-class callable syntax: real use cases
PHP 8.1 first-class callable syntax: strlen(...) instead of Closure::fromCallable("strlen") or fn($x) => strlen($x).
I use it occasionally but feel like I am missing good use cases. What are people actually doing with it?
array_map(strlen(…), $strings) is cleaner than array_map(“strlen”, $strings) (string callables are not type-checked) or array_map(fn($s) => strlen($s), $strings). The … syntax is statically analyzable.
Passing method references: usort($items, $this, “compareByDate”) vs the older [$this, “compareByDate”] array callable. PHPStan catches typos in method names with the new syntax.
Pipeline/composition patterns: $pipeline = [trim(…), strtolower(…), htmlspecialchars(…)]; array_reduce($pipeline, fn($carry, $fn) => $fn($carry), $input).
Event listener registration where you previously used string method names. The static analysis benefit is the main reason to prefer it over strings.
One limitation: you cannot partially apply arguments with the … syntax alone. It captures the callable as-is. For partial application you still need a closure wrapper or a bind helper.
```php blocks are runnable.