← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Arrow Functions fn() => (PHP 7.4)

php PHP 7.4+ Beginner
debt(d5/e1/b1/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

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.

e1 Effort Remediation debt — work required to fix once spotted

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.

b1 Burden Structural debt — long-term weight of choosing wrong

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.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

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.

About DEBT scoring →

TL;DR

PHP 7.4 arrow functions (fn($x) => $x * $factor) are short closures that automatically capture outer scope — no more use($var) boilerplate.

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

Arrow functions capture by reference automatically — they capture by value only. Use traditional closures with use(&$var) for reference capture.

Why It Matters

Arrow functions eliminate the boilerplate of use() in short callbacks — the #1 friction point with PHP closures in array_map/filter chains.

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

✗ Vulnerable
$multiplier = 3;
$doubled = array_map(function($x) use ($multiplier) {
    return $x * $multiplier;
}, $numbers);
✓ Fixed
$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
);

Added 23 Mar 2026
Views 19
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 7 Unknown AI 3 Perplexity 3 ChatGPT 1 Google 1 Ahrefs 1
crawler 15 pre-tracking 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Replace short function($x) use($var) { return expr; } with fn($x) => expr. Rector handles this migration automatically.
📦 Applies To
PHP 7.4+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
function\(.*\) use \(
Auto-detectable: ✓ Yes rector phpcs
🤖 AI Agent
Confidence: High False Positives: Medium ✓ Auto-fixable Fix: Low Context: Line

✓ schema.org compliant