DesignPatternsPHP/DesignPatternsPHP · error · Exception
no exist variable: $name
Error message
no exist variable: $name
What it means
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.
Source
Thrown at Behavioral/Interpreter/Context.php:16
<?php
declare(strict_types=1);
namespace DesignPatterns\Behavioral\Interpreter;
use Exception;
class Context
{
private array $poolVariable;
public function lookUp(string $name): bool
{
if (!key_exists($name, $this->poolVariable)) {
throw new Exception("no exist variable: $name");
}
return $this->poolVariable[$name];
}
public function assign(VariableExp $variable, bool $val)
{
$this->poolVariable[$variable->getName()] = $val;
}
}
View on GitHub (pinned to 54254e0f2a)
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
Example fix
// before
$context->lookUp('x'); // throws: no exist variable: x
// after
$context->assign(new VariableExp('x'), true);
$context->lookUp('x'); Defensive patterns
Strategy: validation
Validate before calling
$vars = (new ReflectionObject($context))->getProperty('poolVariable')->getValue($context);
if (!array_key_exists('x', $vars)) { $context->assign(new VariableExp('x'), true); } Type guard
function hasVariable(Context $context, string $name): bool {
$prop = new ReflectionObject($context)->getProperty('poolVariable');
$prop->setAccessible(true);
return array_key_exists($name, $prop->getValue($context));
} Try / catch
try {
$value = $context->lookUp('x');
} catch (Exception $e) {
$value = false; // default for unassigned variable
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid state given
- Cannot unserialize singleton
- Unknown format given
- Invalid PostId given
- Invalid state given!
AI-assisted analysis of DesignPatternsPHP/DesignPatternsPHP@54254e0f2a (2026-09-01).
Data as JSON: /api/errors/91193f3fadb29ef0.
Report an issue: GitHub.