Arrow Functions fn() => (PHP 7.4)
debt(d5/e1/b1/t5)
Closest to 'specialist tool catches it' (d5). The term's detection_hints list Rector and phpcs — both specialist tools rather than default linters. The code_pattern `function\(.*\) use \(` is what these tools scan for to flag old-style closures that could be modernised to arrow functions, so misuse (sticking with verbose closures or misusing arrow functions) is caught by these tools, not by the compiler or a default linter.
Closest to 'one-line patch or single-call swap' (e1). The quick_fix explicitly states: replace `function($x) use($var) { return expr; }` with `fn($x) => expr`, and notes that Rector handles this migration automatically. This is a mechanical, single-expression substitution.
Closest to 'minimal commitment' (b1). Arrow functions are a localised syntactic feature used in short callbacks. They don't impose an architectural commitment or a persistent tax on maintainers — applies_to covers web/cli/queue but the choice is scoped to individual callback sites, not load-bearing across the system.
Closest to 'notable trap' (t5). The misconception field explicitly states: developers expect arrow functions to capture by reference automatically, but they capture by value only. This is a documented gotcha (the PHP manual calls it out) that most PHP developers eventually encounter, but it doesn't contradict an analogous concept in PHP itself — it's a well-known single gotcha, placing it at t5.
TL;DR
Explanation
fn($params) => expression is a short closure: (1) single expression only — no block body, (2) automatically captures all outer-scope variables by value (no use() needed), (3) cannot capture by reference, (4) returns the expression result implicitly. Use for: array_map/filter callbacks, short transformations, simple callbacks. Still use function(){} for: multi-statement, capturing by reference, recursive closures (can't self-reference in arrow fn). Arrow functions are implemented as the Closure class — they're the same type, just shorter syntax.
Common Misconception
Why It Matters
Common Mistakes
- Expecting multi-statement arrow functions — only one expression, no {}.
- Trying to capture by reference — use traditional function for that.
- Not knowing arrow functions are available in PHP 7.4+, not 8.0+.
Code Examples
$multiplier = 3;
$doubled = array_map(function($x) use ($multiplier) {
return $x * $multiplier;
}, $numbers);
$multiplier = 3;
$doubled = array_map(fn($x) => $x * $multiplier, $numbers);
// Nested arrow functions:
$pipeline = array_map(
fn($item) => array_filter($item->tags, fn($tag) => $tag->active),
$items
);