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.
benmorel/weakmap-polyfill provides a WeakMap implementation for PHP 7.4, filling a gap in the language’s standard library. This is particularly valuable in scenarios where:
Illuminate\Support\Facades\Cache) could benefit from WeakMap to reduce memory leaks in long-running processes (e.g., queues, scheduled jobs, or Lumen micro-services).WeakMap, so adoption is only relevant for legacy PHP 7.4 projects or environments where upgrading is infeasible.WeakMap interface, ensuring seamless integration with existing codebases. Key methods (offsetGet, offsetSet, offsetExists, etc.) align with SplObjectStorage and native WeakMap.WeakMap) could negate the intended benefit. Requires developer education.unserialize() or clone() (if WeakMap instances are serialized).WeakMap provide the most value? (e.g., service container, caching, event listeners?)SplObjectStorage or custom implementations suffice? Are there other polyfills (e.g., php-weakmap) with broader adoption?WeakMap correctly (e.g., avoiding strong reference pitfalls)?WeakMap to avoid memory bloat in long-lived containers.WeakMap to store event listeners or callbacks that should be garbage-collected when their targets are no longer referenced.array, file) to store weak references to objects, enabling automatic cleanup.WeakMap for temporary storage.composer.json constraints to enforce this.WeakMap adoption.WeakMap in a non-critical module (e.g., a custom caching layer or job queue).static or global caches with WeakMap-backed alternatives.pestphp/pest or phpunit with PHP 7.4 to maintain test coverage.WeakMap, so existing code using WeakMap in PHP 8.0+ can migrate with minimal changes.app['key'] caching with WeakMap where appropriate.WeakMap storage (e.g., a custom WeakMapStore).WeakMap for listener storage.WeakMap instances are serialized (e.g., via Laravel’s session or cache), behavior may differ from native WeakMap. Document limitations.WeakMap are not accidentally kept alive by closures or other strong references.composer.json and update dependencies.LaravelWeakMap) to abstract WeakMap usage for consistency.WeakMapContainer).WeakMap").LaravelWeakMap) will require maintenance as Laravel evolves.WeakMap semantics to avoid misuse (e.g., storing objects that should remain strongly referenced).WeakMap behavior with unserialize) and provide fallback strategies.WeakMap is process-local, so it won’t interfere with horizontal scaling (e.g., multiple PHP-FPM workers or Laravel Horizon queues).WeakMap in production if issues arise.WeakMap. Plan for:
How can I help you explore Laravel packages today?