vova5 Dec 2025 06:42

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?

Replies (5)
alex_petrov5 Dec 2025 07:10

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.

0
sergey_web5 Dec 2025 07:14

Passing method references: usort($items, $this, “compareByDate”) vs the older [$this, “compareByDate”] array callable. PHPStan catches typos in method names with the new syntax.

0
nphp5 Dec 2025 07:44

Pipeline/composition patterns: $pipeline = [trim(…), strtolower(…), htmlspecialchars(…)]; array_reduce($pipeline, fn($carry, $fn) => $fn($carry), $input).

0
ivan_morozov5 Dec 2025 08:17

Event listener registration where you previously used string method names. The static analysis benefit is the main reason to prefer it over strings.

0
vova5 Dec 2025 10:01

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.

0
Write a reply
Markdown. ```php blocks are runnable.