<?php
$map = new *WeakMap*;
$obj = new stdClass;

$map[$obj] = $dataForObj;
unset($obj); // $obj automatically dropped from WeakMap! 


Use Case


<?php
class FooBar {
    private WeakMap $cache;

    public function getSomethingWithCaching(object $obj) {
        return $this->cache[$obj] ??= $this->computeSomethingExpensive($obj);
    }

?>