compact / extract
compact(mixed ...$var_names): array — extract(array &$array, int $flags = EXTR_OVERWRITE, string|null $prefix = null): int
A two-way bridge between variables and arrays: compact packs variables into an array by name; extract imports array keys into the current symbol table as variables. extract overwrites same-named variables by default — never use it on untrusted data.
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
// C has no runtime symbol table: a struct is the static "pack"
struct packed { char name[16]; char lang[8]; } p = { .name = "Ada", .lang = "PHP" };
char lang[8], year[8];
strcpy(lang, p.lang); // "extract" is reading fields back into locals, one by one
strcpy(year, "2026");
printf("{\"name\": \"%s\", \"lang\": \"%s\"}\n", p.name, p.lang);
printf("%s %s\n", lang, year);
return 0;
}
{"name": "Ada", "lang": "PHP"}
PHP 2026
C++ C++26
#include <iostream>
#include <string>
int main() {
// no runtime symbol table: an aggregate "packs", structured bindings "extract"
struct Packed { std::string name; std::string lang; } packed{"Ada", "PHP"};
auto [n, l] = packed; // structured bindings unpack fields into fresh locals
std::string year = "2026";
std::cout << "{\"name\": \"" << packed.name << "\", \"lang\": \"" << packed.lang << "\"}\n";
std::cout << l << " " << year << "\n";
return 0;
}
{"name": "Ada", "lang": "PHP"}
PHP 2026
C# v11.0.100
var name = "Ada";
var lang = "PHP";
var packed = new { name, lang }; // projection packs variables by name
Console.WriteLine($"{{\"name\": \"{packed.name}\", \"lang\": \"{packed.lang}\"}}");
// no symbol-table extract: deconstruct into explicitly declared locals instead
var (n, l) = (packed.name, "Python");
var year = 2026;
Console.WriteLine($"{l} {year}");
{"name": "Ada", "lang": "PHP"}
Python 2026
Go v1.27.4
package main
import "fmt"
func main() {
name, lang := "Ada", "PHP"
packed := map[string]any{"name": name, "lang": lang} // no symbol table: pack by hand
fmt.Printf("{\"name\": \"%s\", \"lang\": \"%s\"}\n", packed["name"], packed["lang"])
year := 2026
l := packed["lang"] // "extract" is just reading the map back
fmt.Println(l, year)
}
{"name": "Ada", "lang": "PHP"}
PHP 2026
Java v27.0.1
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String name = "Ada", lang = "PHP";
Map<String, Object> packed = new LinkedHashMap<>(); // no symbol table: explicit map
packed.put("name", name);
packed.put("lang", lang);
System.out.printf("{\"name\": \"%s\", \"lang\": \"%s\"}%n", packed.get("name"), packed.get("lang"));
int year = 2026;
String l = (String) packed.get("lang"); // reading back is the only "extract"
System.out.println(l + " " + year);
}
}
{"name": "Ada", "lang": "PHP"}
PHP 2026
JavaScript v26.13.1
const name = "Ada";
const lang = "PHP";
const packed = { name, lang }; // shorthand property names pack variables
console.log(JSON.stringify(packed));
// destructuring extracts into NEW bindings; the const above is never clobbered
const { year = 2026, lang: lang2 = "?" } = { year: 2026, lang: "Python" };
console.log(lang, lang2, year);
{"name":"Ada","lang":"PHP"}
PHP Python 2026
PHP v8.6.0
$name = "Ada";
$lang = "PHP";
// compact packs existing variables into an array, looked up by name
echo json_encode(compact("name", "lang")), "\n";
// extract imports keys into the local symbol table; EXTR_OVERWRITE is the default, so $lang is clobbered!
extract(["year" => 2026, "lang" => "Python"]);
echo "$lang $year\n";
{"name":"Ada","lang":"PHP"}
Python 2026
Python v3.15.2
def pack():
name = "Ada"
lang = "PHP"
return vars().copy() # vars() snapshots the local symbol table (function scope only)
packed = pack()
print(packed)
# no safe "extract": writing locals() back does NOT create variables reliably;
# explicit per-key reads are the modern, predictable analogue
data = {"year": 2026, "lang": "Python"}
year = data["year"]
lang = data["lang"]
print(lang, year)
{'name': 'Ada', 'lang': 'PHP'}
Python 2026
Rust v1.99.1
use std::collections::HashMap;
fn main() {
let name = "Ada";
let lang = "PHP";
let mut packed: HashMap<&str, &str> = HashMap::new(); // no symbol table: explicit map
packed.insert("name", name);
packed.insert("lang", lang);
println!(
"{{\"name\": \"{}\", \"lang\": \"{}\"}}",
packed["name"],
packed["lang"]
);
let year = 2026;
let l = packed["lang"]; // reading back is the only "extract"
println!("{} {}", l, year);
}
{"name": "Ada", "lang": "PHP"}
PHP 2026
TypeScript v26.13.1
const name = "Ada";
const lang = "PHP";
const packed = { name, lang }; // shorthand property names pack variables
console.log(JSON.stringify(packed));
// destructuring extracts into NEW bindings; the const above is never clobbered
const { year = 2026, lang: lang2 = "?" } = { year: 2026, lang: "Python" };
console.log(lang, lang2, year);
{"name":"Ada","lang":"PHP"}
PHP Python 2026
Language differences
C: no runtime symbol table — a struct is the static "pack", and reading fields back into locals is the "extract".
C++: structured bindings 'auto [a, b] = s' are the closest compile-time analogue of extract, binding brand-new variables.
C#: anonymous-type projections pack by name; tuple deconstruction declares new variables explicitly — symbol-table pollution is impossible.
Go: a 'map[string]any' packs by hand; reading back is the "extract".
Java: an explicit Map; no symbol-table operations exist.
JavaScript: object-literal shorthand ~= compact, destructuring ~= extract; but destructuring only creates new bindings and never clobbers existing variables — extract's most dangerous default (EXTR_OVERWRITE silently overwriting same-named variables) simply does not exist in JS; the JS-side analogue of that risk is dynamic property writes like 'window[data]'.
PHP: compact packs variables by name (an array of names also works); extract imports keys into the current symbol table and returns the import count — with the default EXTR_OVERWRITE it silently overwrites same-named variables (in the sample, $lang changes from "PHP" to "Python") — never extract untrusted input (symbol-table pollution / variable-overwrite attacks); mitigate with flags like 'EXTR_SKIP'/'EXTR_IF_EXISTS'/'EXTR_PREFIX_ALL'; modern code should prefer explicit array reads.
Python: 'vars()' only snapshots the local symbol table inside a function (a half-counterpart); writing locals() back does not create variables; explicit per-key reads are the modern, safe analogue.
Rust: no symbol table — an explicit HashMap.
TypeScript: same as JavaScript.