Constructor
__construct()
Create a new Context instance.
Variable Management
get(string $name): mixed
Get a context variable by name.
Variable name (without $)
Variable value
InvalidArgumentException if the variable is not found.
getAll(): array
Get all defined variables.
Includes both regular scope variables and magic variables (_e, this, etc.).
Associative array of all variables
getSpecialVariables(): array
Get all defined magic variables: _e, __class, $__file, etc.
Associative array of magic variables
setAll(array $vars): void
Set all scope variables.
This method does not set magic variables (_e, __class, etc.).
Associative array of variable names to values
Return Value
setReturnValue($value): void
Set the most recent return value.
This value is available as the $_ magic variable.
Return value to store
getReturnValue(): mixed
Get the most recent return value.
Most recent return value
Exception Handling
setLastException(Throwable $e): void
Set the most recent exception or error.
This value is available as the $_e magic variable.
Exception or error to store
getLastException(): ?Throwable
Get the most recent exception or error.
Most recent exception, or null
InvalidArgumentException if no exception has been caught.
Output Capturing
setLastStdout(string $lastStdout): void
Set the most recent output from evaluated code.
This value is available as the $__out magic variable.
Output string to store
getLastStdout(): ?string
Get the most recent output from evaluated code.
Most recent output, or null
InvalidArgumentException if no output has happened yet.
Object and Class Binding
setBoundObject($boundObject): void
Set the bound object ($this variable) for the interactive shell.
This makes the object available as $this in the REPL, allowing access to private and protected members.
Note: This unsets the bound class, if any exists.
Object to bind, or null to unbind
getBoundObject(): ?object
Get the bound object ($this variable) for the interactive shell.
Bound object, or null
setBoundClass($boundClass): void
Set the bound class (self) for the interactive shell.
This makes the class available as self in the REPL, allowing access to private and protected static members.
Note: This unsets the bound object, if any exists.
Fully qualified class name, or null to unbind
getBoundClass(): ?string
Get the bound class (self) for the interactive shell.
Bound class name, or null
Command Scope Variables
setCommandScopeVariables(array $commandScopeVariables): void
Set command-scope magic variables: __file, $__method, etc.
These are typically set by PsySH commands like whereami and show.
Array of command scope variables
getCommandScopeVariables(): array
Get command-scope magic variables.
Array of command scope variables
getUnusedCommandScopeVariableNames(): array
Get unused command-scope magic variable names.
This is used by the shell to unset old command-scope variables after a new batch is set.
Array of unused variable names
Static Methods
isSpecialVariableName(string $name): bool
Check whether a variable name is a magic variable.
Variable name (without $)
True if the name is a special/magic variable
Magic Variables
The Context class manages these special variables:| Variable | Description |
|---|---|
$_ | Most recent return value |
$_e | Most recent exception |
$__out | Most recent stdout output |
$this | Bound object |
$__function | Current function name (command scope) |
$__method | Current method name (command scope) |
$__class | Current class name (command scope) |
$__namespace | Current namespace (command scope) |
$__file | Current file path (command scope) |
$__line | Current line number (command scope) |
$__dir | Current directory (command scope) |