{
    "slug": "php5_closures_intro",
    "term": "Anonymous Functions / Closures (PHP 5.3)",
    "category": "php",
    "difficulty": "intermediate",
    "short": "PHP 5.3 introduced anonymous functions (closures) — function() {} expressions assignable to variables, passable as callbacks, and able to capture outer scope with use().",
    "long": "PHP 5.3 (2009) added: anonymous functions: $fn = function($x) { return $x * 2; }. Scope capture with use: function() use ($var) — captures by value. use (&$var) captures by reference. Closures are instances of the Closure class. PHP 7.4 added arrow functions: fn($x) => $x * 2 — automatically capture outer scope. Closures can be bound to different objects with Closure::bind(). In PHP 8.1, closures work with first-class callable syntax: strlen(...).",
    "aliases": [],
    "tags": [
        "php",
        "closures",
        "php5",
        "functional"
    ],
    "misconception": "Closures automatically capture all outer variables — they don't. You must explicitly list captured variables in use($var). Arrow functions (PHP 7.4) capture automatically.",
    "why_it_matters": "Closures enable functional programming patterns, callback-based APIs, and deferred execution — foundational to modern PHP frameworks and middleware stacks.",
    "common_mistakes": [
        "Forgetting use() to capture outer variables — variable not available in closure.",
        "Using use(&$var) when use($var) suffices — reference capture causes subtle bugs.",
        "Not knowing arrow functions auto-capture — saves use() boilerplate."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php5_oop_model",
        "php_callable_types",
        "arrow_functions",
        "php_array_functions_fp"
    ],
    "prerequisites": [
        "php5_oop_model"
    ],
    "refs": [
        "https://www.php.net/manual/en/functions.anonymous.php"
    ],
    "bad_code": "$multiplier = 3;\n$fn = function($x) {\n    return $x * $multiplier; // Error: $multiplier not defined (not captured)\n};",
    "good_code": "$multiplier = 3;\n\n// PHP 5.3+ — explicit capture:\n$fn = function($x) use ($multiplier) {\n    return $x * $multiplier;\n};\n\n// PHP 7.4+ — auto-capture with arrow function:\n$fn = fn($x) => $x * $multiplier;",
    "quick_fix": "Add use($var) to capture outer variables in closures. Upgrade to PHP 7.4 arrow functions for single-expression closures that auto-capture scope.",
    "severity": "info",
    "effort": "low",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php5_closures_intro",
        "html_url": "https://codeclaritylab.com/glossary/php5_closures_intro",
        "json_url": "https://codeclaritylab.com/glossary/php5_closures_intro.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": "[Anonymous Functions / Closures (PHP 5.3)](https://codeclaritylab.com/glossary/php5_closures_intro) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php5_closures_intro"
            }
        }
    }
}