← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

WeakMap (PHP 8.0)

php PHP 8.0+ Advanced

Also Known As

WeakMap PHP 8 WeakMap weak map PHP

TL;DR

A map keyed by objects that doesn't prevent garbage collection — ideal for caching per-object computed data without creating memory leaks.

Explanation

A WeakMap holds object keys weakly — when the only remaining reference to a key object is in the WeakMap, that object is garbage collected and its WeakMap entry is automatically removed. This contrasts with SplObjectStorage and arrays, which hold strong references and prevent GC. Typical use cases: per-object caches (memoising computed values on an entity without modifying the entity class), attribute caches in ORMs, and proxy metadata storage. WeakMap is iterable and Countable. PHP 8.0+; the RFC was specifically motivated by Doctrine's need to associate metadata with entities without preventing their deallocation.

Common Misconception

WeakMap and WeakReference serve the same purpose. WeakReference is a pointer to an object that does not prevent GC. WeakMap is a key-value store keyed on objects where entries are automatically removed when the key object is garbage collected — ideal for associating metadata with objects without preventing their cleanup.

Why It Matters

WeakMap (PHP 8.0) stores key-value pairs keyed by objects without preventing garbage collection — perfect for caches and memoization where objects should not be kept alive solely because they are cache keys.

Common Mistakes

  • Using a regular array or SplObjectStorage for object-keyed caches — holds strong references, preventing GC.
  • Not checking if a WeakMap key still exists before access — the object may have been collected.
  • Using WeakMap for non-object keys — it only accepts objects as keys.
  • Not realising WeakMap is not iterable when the GC runs between iterations — do not iterate a WeakMap.

Code Examples

✗ Vulnerable
// Regular array cache — prevents GC of $user objects:
class Cache {
    private array $data = [];
    public function set(object $key, mixed $val): void {
        $this->data[spl_object_id($key)] = $val; // Strong reference via ID
    }
}

// WeakMap — GC can collect $user when no other references exist:
$cache = new WeakMap();
$cache[$user] = computeExpensiveData($user); // Freed when $user is GC'd
✓ Fixed
$cache = new WeakMap();
function expensiveData(object $obj, WeakMap $cache): array {
    return $cache[$obj] ??= computeExpensiveData($obj);
    // Entry removed automatically when $obj is garbage collected
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 24
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 1 ping F 0 pings S
No pings yet today
Amazonbot 7 Perplexity 3 SEMrush 3 Unknown AI 2 Ahrefs 2 Google 1
crawler 18
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use WeakMap when you need to associate data with object instances without preventing garbage collection — ideal for caches keyed by object references
📦 Applies To
PHP 8.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
SplObjectStorage or array of objects preventing GC; memoization cache keyed by object reference with no cleanup
Auto-detectable: ✗ No phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: Medium ✗ Manual fix Fix: Medium Context: Class

✓ schema.org compliant