3v4l.org

run code in 300+ PHP versions simultaneously
<?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...');

preferences:
25.16 ms | 406 KiB | 5 Q