get_defined_vars
get_defined_vars(): array
Returns every defined variable in the current scope (in a function: parameters plus in-body variables; globally, superglobals too) - a dynamic-language introspection feature.
C C23
#include <stdio.h>
int main(void) {
/* Static scope: the variable table is a compile-time symbol table;
no runtime introspection exists (debuggers read DWARF instead) */
const char *x = "a";
int y = 2;
printf("x = %s\n", x);
printf("y = %d\n", y);
return 0;
}
x = a y = 2
C++ C++26
#include <iostream>
#include <string>
int main() {
// Static scope: names are erased at runtime; debuggers read DWARF instead
std::string x = "a";
int y = 2;
std::cout << "x = " << x << "\n";
std::cout << "y = " << y << "\n";
}
x = a y = 2
C# v11.0.100
// Static scope: locals live on the IL stack, names erased at runtime;
// print a hand-built table instead
string x = "a";
int y = 2;
Console.WriteLine("x = " + x);
Console.WriteLine("y = " + y);
x = a y = 2
Go v1.27.4
package main
import "fmt"
func main() {
// Static scope: the variable table is a compile-time artifact (DWARF
// debug info); no runtime introspection - print a hand-built table
x, y := "a", 2
entries := []struct {
name string
val any
}{{"x", x}, {"y", y}}
for _, e := range entries {
fmt.Println(e.name, "=", e.val)
}
}
x = a y = 2
Java v27.0.1
public class Main {
public static void main(String[] args) {
// Static scope: locals are erased (LocalVariableTable is debug-only);
// print a hand-built table instead
String x = "a";
int y = 2;
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}
x = a y = 2
JavaScript v26.13.1
// No equivalent: lexical scope resolves statically at parse time.
// arguments carries parameters only, never the whole table
function demo(x) {
const y = 2;
console.log(arguments.length, x, y);
}
demo("hi");
1 hi 2
PHP v8.6.0
// Function scope: only user-defined variables come back (manual: parameters + in-body definitions)
function demo(string $x): void {
$y = 2;
$z = [1, 2];
echo json_encode(array_keys(get_defined_vars())), "\n";
echo count(get_defined_vars()), "\n";
// The returned array is a value copy: mutating it does not write back
$vars = get_defined_vars();
$vars['y'] = 99;
echo $y;
}
demo('hello');
["x","y","z"] 3 2
Python v3.15.2
def demo(x):
y = 2
# locals() is the closest equivalent: a dict snapshot of current names
print(sorted(locals()), len(locals()))
snapshot = locals()
print(snapshot["x"], snapshot["y"])
demo("hi")
['x', 'y'] 2 hi 2
Rust v1.99.1
fn main() {
// Static scope: names are erased at compile time; no runtime variable
// table exists - dump a hand-built list instead
let x = "a";
let y = 2;
let table: [(&str, String); 2] = [("x", x.to_string()), ("y", y.to_string())];
for (name, val) in &table {
println!("{} = {}", name, val);
}
}
x = a y = 2
TypeScript v26.13.1
// No equivalent: lexical scope resolves statically at parse time.
// rest parameters stand in for arguments under strict TS
function demo(...args: unknown[]): void {
const y = 2;
console.log(args.length, args[0], y);
}
demo("hi");
1 hi 2
Language differences
C: static scope - the variable table is a compile-time symbol table, no runtime introspection exists; debuggers see variables via DWARF debug info, not a runtime API.
C++: same as C; names are erased at runtime, DWARF/PDB restores them only under a debugger.
C#: no equivalent - locals live on the IL stack with names erased at runtime; reflection reaches object members (fields/properties) but never a method's local variable table.
Go: static scope, compiled - reflect reaches structs but not function locals; the variable table is a compile-time artifact (DWARF).
Java: no equivalent - locals are erased; LocalVariableTable is a debug attribute (javac -g) for debuggers only, unreachable from runtime APIs.
JavaScript: lexical scope cannot do this - the scope chain is fixed at parse time and no API enumerates the current scope's names; arguments carries parameters only. This entry shows it honestly: the capability belongs to dynamic languages PHP/Python.
PHP: a dynamic-language feature - inside a function it returns parameters plus in-body definitions (manual positioning); globally it includes superglobals (_SERVER etc.) too; the return is a value copy, mutations do not write back.
Python: locals() is the closest - a dict snapshot of the current scope's names (in a function: parameters plus in-body variables); since CPython 3.13 locals() returns a fresh snapshot each call, and mutating the snapshot does not write back.
Rust: no equivalent - statically compiled, names and scopes fully erased at compile time.
TypeScript: same as JavaScript (no lexical-scope equivalent); strict mode uses rest parameters instead of arguments.