{
    "slug": "symbol_table_resolution",
    "term": "Symbol Table Resolution",
    "category": "compiler",
    "difficulty": "advanced",
    "short": "The compiler phase that maps identifiers (variables, functions, classes) to their declarations, scopes, and types during semantic analysis.",
    "long": "Namespaces add a separate resolution pass: unqualified class names resolve against `use` imports, then the current namespace, with no global fallback - which is why a missing `use` produces 'class not found' even when the class exists globally. Unqualified function and constant names, by contrast, do fall back to the global namespace.",
    "aliases": [
        "name resolution",
        "name binding",
        "scope resolution",
        "identifier binding"
    ],
    "tags": [
        "compiler",
        "name-resolution",
        "scoping",
        "semantic-analysis",
        "php"
    ],
    "misconception": "Symbol resolution is just a lookup that happens once the parser finishes. In reality it is a distinct semantic phase that tracks nested scopes and can fail with errors the parser never sees, like undefined variables or unresolved imports.",
    "why_it_matters": "Resolution errors are the most common compile-time failures developers hit - missing imports, typos in names, shadowed variables - and understanding scope rules prevents subtle bugs where a name silently binds to the wrong declaration.",
    "common_mistakes": [
        "Assuming variables have block scope in PHP - they are function-scoped, so a variable declared inside an if block leaks to the rest of the function.",
        "Forgetting that closures do not capture outer variables automatically in PHP and must list them in a use() clause.",
        "Omitting a use statement and expecting an unqualified class name to resolve globally - it resolves against the current namespace first.",
        "Shadowing an outer variable with a same-named inner one and being surprised the outer value is unchanged.",
        "Relying on case-insensitive function and class names while assuming variable names are also case-insensitive."
    ],
    "when_to_use": [
        "Reach for explicit use() capture lists whenever a closure must read or mutate outer variables across PHP's function scope boundary.",
        "Use namespace imports and fully qualified names deliberately when resolving classes so the compiler binds the intended declaration."
    ],
    "avoid_when": [
        "Do not manually reason about complex namespace resolution when a static analyser like PHPStan or Psalm can flag unresolved names automatically.",
        "Avoid relying on global fallback resolution for classes - it never happens, so always import classes explicitly with use."
    ],
    "related": [
        "abstract_syntax_tree",
        "lexer_parser",
        "type_inference",
        "php_compilation_pipeline"
    ],
    "prerequisites": [
        "abstract_syntax_tree",
        "lexer_parser",
        "php_compilation_pipeline"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Name_resolution_(programming_languages)",
        "https://www.php.net/manual/en/language.namespaces.rules.php",
        "https://www.php.net/manual/en/language.variables.scope.php"
    ],
    "bad_code": "$count++; // $count is undefined here; warns, treats null as 0, returns 1 each call",
    "good_code": "<?php\nnamespace App;\n\nuse Monolog\\Logger;\n\nfunction makeCounter() {\n    $count = 0;\n    // Bind $count by reference so the closure shares the same symbol\n    return function () use (&$count) {\n        $count++;\n        return $count;\n    };\n}\n$next = makeCounter();\necho $next(); // 1\necho $next(); // 2 - resolution captured the outer binding\n\n$logger = new Logger('app'); // resolves to Monolog\\Logger",
    "quick_fix": "Track which scope each identifier lives in - add missing use imports for classes, list captured variables in closure use() clauses, and rename shadowed variables to avoid silent rebinding.",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-06-06",
    "updated": "2026-06-06",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/symbol_table_resolution",
        "html_url": "https://codeclaritylab.com/glossary/symbol_table_resolution",
        "json_url": "https://codeclaritylab.com/glossary/symbol_table_resolution.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": "[Symbol Table Resolution](https://codeclaritylab.com/glossary/symbol_table_resolution) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/symbol_table_resolution"
            }
        }
    }
}