{
    "slug": "arrow_functions",
    "term": "Arrow Functions (PHP 7.4)",
    "category": "php",
    "difficulty": "beginner",
    "short": "Concise single-expression anonymous functions using fn() that implicitly capture outer-scope variables by value.",
    "long": "Arrow functions (fn($x) => $x * 2) are a shorthand for closures that evaluate a single expression and implicitly capture outer-scope variables by value — no use clause needed. They are particularly useful with array functions like array_map() and array_filter(). Arrow functions cannot capture by reference, cannot span multiple statements, and cannot use yield. They return the value of the expression automatically.",
    "aliases": [
        "PHP arrow functions",
        "fn()",
        "short closures"
    ],
    "tags": [
        "php",
        "php7-4",
        "functional",
        "closures"
    ],
    "misconception": "Arrow functions and closures are identical features with different syntax. Arrow functions implicitly capture outer scope variables by value without needing use() — but they cannot capture by reference or modify outer variables, which regular closures can do with use(&$var).",
    "why_it_matters": "Arrow functions capture outer scope variables automatically without use() — they make short callbacks dramatically more readable and eliminate the most common closure mistake of forgetting to import variables.",
    "common_mistakes": [
        "Using arrow functions for multi-line logic — they only support a single expression, use regular closures for complex bodies.",
        "Expecting to modify outer variables — arrow functions capture by value, mutations do not propagate outward.",
        "Using them for class methods — arrow functions are for callbacks and closures, not method definitions.",
        "Nesting arrow functions three or four levels deep — readability collapses fast, extract named functions instead."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "closures",
        "array_functions"
    ],
    "prerequisites": [
        "closures",
        "php_data_types",
        "array_functions"
    ],
    "refs": [
        "https://www.php.net/manual/en/functions.arrow.php"
    ],
    "bad_code": "// Verbose closure when arrow function suffices:\n$multiplier = 3;\n$result = array_map(function($n) use ($multiplier) {\n    return $n * $multiplier;\n}, $numbers);\n\n// Better: $result = array_map(fn($n) => $n * $multiplier, $numbers);",
    "good_code": "// Arrow functions (PHP 7.4) — implicit capture, single expression\n$multiplier = 10;\n\n// Traditional closure — requires explicit 'use'\n$fn1 = function(int $n) use ($multiplier): int { return $n * $multiplier; };\n\n// Arrow function — captures $multiplier automatically\n$fn2 = fn(int $n): int => $n * $multiplier;\n\n// Great for array operations\n$prices = array_map(fn(Product $p) => $p->price * 1.2, $products);\n\n$active = array_filter($users, fn(User $u) => $u->isActive() && !$u->isBanned());\n\n// Chained transformations\n$result = array_map(\n    fn($item) => $item->total * (1 - $item->discountRate),\n    array_filter($items, fn($item) => $item->status === 'paid')\n);",
    "quick_fix": "Use arrow functions (fn($x) => $x * 2) instead of function($x) use ($var) { return $x * $var; } — they automatically capture outer scope without explicit use keyword",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/arrow_functions",
        "html_url": "https://codeclaritylab.com/glossary/arrow_functions",
        "json_url": "https://codeclaritylab.com/glossary/arrow_functions.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Arrow Functions (PHP 7.4)](https://codeclaritylab.com/glossary/arrow_functions) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/arrow_functions"
            }
        }
    }
}