PHPackages                             benmorel/weakmap-polyfill - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. benmorel/weakmap-polyfill

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

benmorel/weakmap-polyfill
=========================

A WeakMap polyfill for PHP 7.4

0.5.0(2y ago)471.5M↓26.7%55MITPHPPHP ^7.4 || ^8.0CI passing

Since Jan 20Pushed 1y ago3 watchersCompare

[ Source](https://github.com/BenMorel/weakmap-polyfill)[ Packagist](https://packagist.org/packages/benmorel/weakmap-polyfill)[ GitHub Sponsors](https://github.com/BenMorel)[ RSS](/packages/benmorel-weakmap-polyfill/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (2)Versions (8)Used By (5)

WeakMap polyfill for PHP 7.4
============================

[](#weakmap-polyfill-for-php-74)

This polyfill aims to be 100% compatible with `WeakMap` in PHP 8.

[![Build Status](https://github.com/BenMorel/weakmap-polyfill/workflows/CI/badge.svg)](https://github.com/BenMorel/weakmap-polyfill/actions)[![Coverage Status](https://camo.githubusercontent.com/1a6b9f03eef56e562d0b31b1023d13325fcec507d3eb86b540903890f71fc206/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f42656e4d6f72656c2f7765616b6d61702d706f6c7966696c6c2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/BenMorel/weakmap-polyfill?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/2bc43032d9e76ea5700da6813c8cec8a84bfac9fe5a47bf8a3531f3350a57f2e/68747470733a2f2f706f7365722e707567782e6f72672f62656e6d6f72656c2f7765616b6d61702d706f6c7966696c6c2f762f737461626c65)](https://packagist.org/packages/benmorel/weakmap-polyfill)[![Total Downloads](https://camo.githubusercontent.com/549c0f24a29307d514c1cbb170482e9db9de07d60f76d5e4efa3895e3e21fc97/68747470733a2f2f706f7365722e707567782e6f72672f62656e6d6f72656c2f7765616b6d61702d706f6c7966696c6c2f646f776e6c6f616473)](https://packagist.org/packages/benmorel/weakmap-polyfill)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](http://opensource.org/licenses/MIT)

Introduction
------------

[](#introduction)

PHP 7.4 introduced `WeakReference`, but didn't include a `WeakMap` implementation. [This has been implemented](https://wiki.php.net/rfc/weak_maps) since, but is only available in PHP 8.

The RFC author, Nikita Popov, highlights why a userland `WeakMap` is suboptimal:

> Weak maps require first-class language support and cannot be implemented using existing functionality provided by PHP.
>
> At first sight, it may seem that an array mapping from spl\_object\_id() to arbitrary values could serve the purpose of a weak map. This is not the case for multiple reasons:
>
> - spl\_object\_id() values are reused after the object is destroyed. Two different objects can have the same object ID – just not at the same time.
> - The object ID cannot be converted back into an object, so iteration over the map is not possible.
> - The value stored under the ID will not be released when the object is destroyed.
>
> Using the WeakReference class introduced in PHP 7.4, it is possible to avoid the first two issues (…). However, this does not solve the third problem: The data will not be released when the object is destroyed. It will only be released on the next access with an object that has the same reused ID, or if a garbage collection mechanism, which performs regular sweeps of the whole map, is implemented.
>
> A native weak map implementation will instead remove the value from the weak map as soon as the object key is destroyed.

This is the trade-off this library offers: a 100% compatible implementation, but:

- slower
- whose values are not removed as soon as the object key is destroyed, but when you use the `WeakMap` again; note that this affects when object destructors are called as well

Here is how it works:

- calls to `count()` will always garbage collect dangling references immediately
- using array-like features: set, get, `isset()`, `unset()` will perform garbage collection at least every 100 operations (less often for large WeakMaps).
- when GC cycles are collected (automatically or via `gc_collect_cycles()`), garbage collection will trigger immediately

This offers a reasonable trade-off between performance and memory usage.

Installation
------------

[](#installation)

This library is installable via [Composer](https://getcomposer.org/):

```
composer require benmorel/weakmap-polyfill
```

Requirements
------------

[](#requirements)

This library requires PHP 7.4 or later.

Quickstart
----------

[](#quickstart)

```
$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
```

Alternatives
------------

[](#alternatives)

The [`weakreference_bc` PECL](https://pecl.php.net/package/weakreference_bc) backports a native polyfill for WeakReference and WeakMap to PHP 7.0-7.4. The PECL has the following advantages:

- `weakreference_bc` supports PHP 7.0+
- Like the real WeakMap/WeakReference, values are removed as soon as the object key is destroyed.
- Native implementations are faster and use less memory.
- `weakreference_bc`'s `WeakMap::count()` is fast because it does not need to perform garbage collection.

This has the following drawbacks:

- Installing a PECL has more steps than installing a composer package.
- `weakreference_bc` currently implements `Iterator` instead of the more accurate `IteratorAggregate` as of 0.4.1.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity51

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~255 days

Recently: every ~383 days

Total

7

Last Release

776d ago

PHP version history (2 changes)0.1.0PHP &gt;=7.4

0.3.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/57189121968030f0770811b461cc92f9c19c08f5c4767292f2ede48b7277cfad?d=identicon)[BenMorel](/maintainers/BenMorel)

---

Top Contributors

[![BenMorel](https://avatars.githubusercontent.com/u/1952838?v=4)](https://github.com/BenMorel "BenMorel (45 commits)")[![TysonAndre](https://avatars.githubusercontent.com/u/1904430?v=4)](https://github.com/TysonAndre "TysonAndre (3 commits)")[![BackEndTea](https://avatars.githubusercontent.com/u/14289961?v=4)](https://github.com/BackEndTea "BackEndTea (2 commits)")[![mabar](https://avatars.githubusercontent.com/u/20974277?v=4)](https://github.com/mabar "mabar (1 commits)")[![mvorisek](https://avatars.githubusercontent.com/u/2228672?v=4)](https://github.com/mvorisek "mvorisek (1 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")

---

Tags

weakrefweakreferenceweakmap

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/benmorel-weakmap-polyfill/health.svg)

```
[![Health](https://phpackages.com/badges/benmorel-weakmap-polyfill/health.svg)](https://phpackages.com/packages/benmorel-weakmap-polyfill)
```

###  Alternatives

[bluebaytravel/dosh

Simple currency formatter for PHP.

2120.8k](/packages/bluebaytravel-dosh)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
