- 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.