{"record":{"id":"91193f3fadb29ef0","repo":"DesignPatternsPHP/DesignPatternsPHP","slug":"no-exist-variable-name","errorCode":null,"errorMessage":"no exist variable: $name","messagePattern":"no exist variable: \\$name","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Behavioral/Interpreter/Context.php","lineNumber":16,"sourceCode":"<?php\n\ndeclare(strict_types=1);\n\nnamespace DesignPatterns\\Behavioral\\Interpreter;\n\nuse Exception;\n\nclass Context\n{\n    private array $poolVariable;\n\n    public function lookUp(string $name): bool\n    {\n        if (!key_exists($name, $this->poolVariable)) {\n            throw new Exception(\"no exist variable: $name\");\n        }\n\n        return $this->poolVariable[$name];\n    }\n\n    public function assign(VariableExp $variable, bool $val)\n    {\n        $this->poolVariable[$variable->getName()] = $val;\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/DesignPatternsPHP/DesignPatternsPHP/blob/54254e0f2a59e27280f81304bce9218e12f97a03/Behavioral/Interpreter/Context.php#L1-L27","documentation":"Context::lookUp() is the Interpreter pattern's variable lookup. It throws a generic Exception when the requested variable name is not present in the context's variable pool, because evaluating a TerminalExpression for an unassigned variable has no defined value.","triggerScenarios":"Calling Context::lookUp('name') before Context::assign() was called for that VariableExp; a misspelled variable name in an expression; evaluating an expression tree against a fresh/empty Context.","commonSituations":"Building interpreter expressions in the wrong order (evaluating before assignments), reusing a new Context object with expressions that reference variables set on an older Context, or typos in variable names.","solutions":["Call $context->assign(new VariableExp('name'), true) before evaluating expressions that reference 'name'","Verify the variable name string matches exactly (case-sensitive) between the expression and the assignment","Reuse the same Context instance across expression construction and evaluation"],"exampleFix":"// before\n$context->lookUp('x'); // throws: no exist variable: x\n// after\n$context->assign(new VariableExp('x'), true);\n$context->lookUp('x');","handlingStrategy":"validation","validationCode":"$vars = (new ReflectionObject($context))->getProperty('poolVariable')->getValue($context);\nif (!array_key_exists('x', $vars)) { $context->assign(new VariableExp('x'), true); }","typeGuard":"function hasVariable(Context $context, string $name): bool {\n    $prop = new ReflectionObject($context)->getProperty('poolVariable');\n    $prop->setAccessible(true);\n    return array_key_exists($name, $prop->getValue($context));\n}","tryCatchPattern":"try {\n    $value = $context->lookUp('x');\n} catch (Exception $e) {\n    $value = false; // default for unassigned variable\n}","preventionTips":["Always assign every variable referenced by your expression tree before evaluation","Reuse a single Context instance for a whole interpretation run","Keep variable names in constants to avoid typos"],"tags":["php","interpreter-pattern","undefined-variable"],"backgroundTag":"undefined-variable-lookup","analyzedSha":"54254e0f2a59e27280f81304bce9218e12f97a03","analyzedAt":"2026-09-01T06:00:26.376Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}