{
    "slug": "php_callable_types",
    "term": "callable vs Closure vs First-Class Callable",
    "category": "php",
    "difficulty": "intermediate",
    "short": "PHP has three callable forms: loose callable (string/array), typed Closure, and PHP 8.1 first-class callables (strlen(...)) — prefer Closure or first-class callables for type safety.",
    "long": "callable is a loose type hint that accepts strings ('strlen'), arrays ([$obj, 'method']), and Closures. It provides no IDE autocompletion or static analysis. Closure is a specific class for anonymous functions — preferred for type-safe callbacks. PHP 8.1 introduced first-class callables: strlen(...) creates a Closure from any callable. Closure::fromCallable() does the same in PHP 7.1+. For strict code: never type-hint callable, always use Closure or a specific interface. PHPStan/Psalm can check Closure parameter types.",
    "aliases": [],
    "tags": [
        "php",
        "closures",
        "php81",
        "callbacks"
    ],
    "misconception": "callable and Closure are the same — callable is a loose type that accepts strings and arrays too. Closure is a specific typed class.",
    "why_it_matters": "callable type hints prevent static analysis from verifying callback signatures — using Closure enables type checking of the callback's parameters and return type.",
    "common_mistakes": [
        "Type-hinting callable instead of Closure — loses static analysis.",
        "Using string-based callables ('strlen') that break with refactoring.",
        "Not knowing strlen(...) syntax (PHP 8.1) for converting built-ins to Closures."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "closures",
        "first_class_callables",
        "arrow_functions",
        "static_analysis"
    ],
    "prerequisites": [
        "closures",
        "first_class_callables"
    ],
    "refs": [
        "https://www.php.net/manual/en/functions.first_class_callable_syntax.php"
    ],
    "bad_code": "function process(callable $callback, array $items): array {\n    return array_map($callback, $items); // No type safety on callback\n}\nprocess('strtolower', $items); // Works but fragile",
    "good_code": "// PHP 7.1+:\n$fn = Closure::fromCallable('strtolower');\n\n// PHP 8.1+ first-class callables:\n$fn = strtolower(...);\n$fn = $obj->method(...);\n$fn = ClassName::staticMethod(...);\n\n// Type-safe callback parameter:\nfunction process(Closure $callback, array $items): array {\n    return array_map($callback, $items);\n}",
    "quick_fix": "Replace callable type hints with Closure. Use strlen(...) syntax (PHP 8.1) or Closure::fromCallable() to convert built-in functions to typed Closures.",
    "severity": "info",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_callable_types",
        "html_url": "https://codeclaritylab.com/glossary/php_callable_types",
        "json_url": "https://codeclaritylab.com/glossary/php_callable_types.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": "[callable vs Closure vs First-Class Callable](https://codeclaritylab.com/glossary/php_callable_types) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_callable_types"
            }
        }
    }
}