Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Weakmap Polyfill

Weakmap Polyfill Laravel Package

benmorel/weakmap-polyfill

Polyfill for PHP WeakMap, providing weakly-referenced key/value storage for older PHP versions. Store data associated with objects without preventing garbage collection. Useful for caches, metadata, and object maps in libraries and frameworks.

View on GitHub
Deep Wiki
Context7

A WeakMap polyfill for PHP 7.4

Frequently asked questions about Weakmap Polyfill
How do I install benmorel/weakmap-polyfill in a Laravel project?
Run `composer require benmorel/weakmap-polyfill` in your project directory. The package is autoloaded via Composer, so no additional configuration is needed unless you’re using PHP 8.0+, where the native WeakMap would replace this polyfill.
Does this polyfill work with Laravel’s service container?
Yes, but use it carefully. Store weakly-referenced data in the container (e.g., for caching bound services) to avoid memory leaks in long-running processes like queues or Lumen workers. Replace manual static caches with WeakMap to let garbage collection handle cleanup.
Will this break if I upgrade to PHP 8.0+?
No, but it’s redundant in PHP 8.0+. The polyfill mimics the native WeakMap API, so upgrading will require removing this package and relying on PHP’s built-in implementation. Test thoroughly to ensure compatibility with your Laravel version.
Can I use WeakMap for Laravel event listeners or closures?
Absolutely. Store event listeners or callbacks in a WeakMap to ensure they’re garbage-collected when their target objects are no longer referenced. This prevents memory leaks in long-lived Laravel processes like scheduled jobs or API gateways.
How does this polyfill handle memory leaks compared to SplObjectStorage?
Unlike SplObjectStorage, this polyfill uses weak references, so keys (objects) won’t prevent garbage collection. However, values are only cleaned up on the next WeakMap access, not immediately when the key is destroyed—unlike PHP 8’s native WeakMap.
Is this safe for production in high-traffic Laravel apps?
Yes, but benchmark first. The polyfill adds overhead, so test in staging with tools like Blackfire or Xdebug to ensure it doesn’t degrade performance in high-throughput systems (e.g., API endpoints or queue workers).
Does this work with Laravel’s caching drivers (e.g., Redis, file)?
No, this polyfill is for in-memory weak references only. Use it alongside Laravel’s cache drivers (e.g., `Cache::put()`) for persistent storage, but combine WeakMap for temporary, object-associated data that should auto-cleanup.
Are there alternatives to this polyfill for Laravel?
For PHP 7.4, this is the most compatible option. Alternatives like `php-weakmap` exist but may lack Laravel-specific optimizations. If you’re stuck on PHP 7.4, this polyfill is the safest choice for WeakMap behavior.
How do I test WeakMap behavior in Laravel unit tests?
Mock object lifecycle in tests by creating objects, storing them in WeakMap, then unsetting references and verifying cleanup. Use PHP’s `gc_collect_cycles()` to force garbage collection and check if WeakMap entries are removed as expected.
Will this polyfill work with Laravel’s queue workers or Horizon?
Yes, but monitor memory usage. WeakMap is ideal for storing job metadata or temporary data in queues, as it prevents leaks when jobs complete. Pair with Laravel’s `dispatch()` and `afterCommit()` hooks for cleanup timing.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky