- var_dump: documentation ( source)
- json_encode: documentation ( source)
- range: documentation ( source)
<?php
$records = [
't' => new WeakMap(),
];
class X {
public array $id;
public function __destruct() { var_dump('Cleaning up: ' . json_encode($this->id)); }
}
// Treat this as usages external to Records itself.
$external = [];
(function () use (&$records, &$external) {
foreach (range(0, 3) as $i) {
$x = new X();
$x->id = [$i];
$records['t'][$x] = WeakReference::create($x);
$external[] = $x;
}
})();
$finder = function (array $find) use (&$records) {
foreach ($records['t'] as $object => $ref) {
if ($object->id == $find) {
return $object;
}
}
};
var_dump('Found object:', $finder([2]));
var_dump('Found object:', $finder([99]));
var_dump(count($records['t']));
var_dump('Removing external 2 (should auto-destruct)');
unset($external[2]);
var_dump(count($records['t']));
var_dump('Exiting...');