Weakmap

WeakMap is a special object type introduced in PHP 8.0.

It is a map, or key–value storage, where the keys are objects and the stored values are automatically removed when the object key is no longer referenced anywhere else in your program.

They may be considered as an associative array, where the keys must be objects. It also doesn’t prevent objects from being garbage-collected.

<?php

    $weakMap = new WeakMap();

    $obj = new stdClass();
    $weakMap[$obj] = 'some data';

    echo $weakMap[$obj]; // some data

    // Now unset $obj:
    unset($obj);

    // The entry is automatically removed from the WeakMap
    var_dump($weakMap); // empty WeakMap

?>

Documentation

See also Hacking PHP’s WeakMap for Value Object D× and WeakMaps a hidden gem in PHP.

Related : Array, SplStorageObjects, Garbage Collection, Resource Leak