PHP code example of benmorel / weakmap-polyfill

1. Go to this page and download the library: Download benmorel/weakmap-polyfill library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

benmorel / weakmap-polyfill example snippets


$weakMap = new WeakMap();

$a = new stdClass();
$b = new stdClass();

$weakMap[$a] = 123;

var_export(isset($weakMap[$a])); // true
var_export(isset($weakMap[$b])); // false

echo $weakMap[$a]; // 123
echo $weakMap[$b]; // Error

echo count($weakMap); // 1

// removing the last reference to the object will remove it from the WeakMap
unset($a);

echo count($weakMap); // 0