SplObjectStorage models a set of objects with optional attached information.
The examples focus on the operation itself, including the important distinction: WeakMap differs because it does not keep keys alive.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Before You Start
Read the input requirements before applying this pattern to production data.
Example 1: Basic Use
Each entry has a strong object key and separate metadata; equivalent objects do not automatically share an entry.
<?php
$a = (object) ["name" => "A"];
$b = (object) ["name" => "B"];
$storage = new SplObjectStorage();
$storage->attach($a, "active");
$storage->attach($b, "pending");
foreach ($storage as $object) { echo $object->name . ":" . $storage->getInfo() . PHP_EOL; }
Output from a successful local PHP 8.4 run:
A:active
B:pending
Example 2: Checking Behavior
Each entry has a strong object key and separate metadata; equivalent objects do not automatically share an entry. The storage also exposes set membership and removal operations.
<?php
$a = (object) ["name" => "A"];
$b = (object) ["name" => "B"];
$storage = new SplObjectStorage();
$storage->attach($a, "active");
$storage->attach($b, "pending");
foreach ($storage as $object) { echo $object->name . ":" . $storage->getInfo() . PHP_EOL; }
echo "count=" . count($storage);
Output from a successful local PHP 8.4 run:
A:active
B:pending
count=2
Example 3: Practical Example
Each entry has a strong object key and separate metadata; equivalent objects do not automatically share an entry. The storage also exposes set membership and removal operations.
<?php
$a = (object) ["name" => "A"];
$b = (object) ["name" => "B"];
$storage = new SplObjectStorage();
$storage->attach($a, "active");
$storage->attach($b, "pending");
foreach ($storage as $object) { echo $object->name . ":" . $storage->getInfo() . PHP_EOL; }
$storage->detach($a);
echo "after detach=" . count($storage);
Output from a successful local PHP 8.4 run:
A:active
B:pending
after detach=1
Important Behavior and Edge Cases
Membership uses identity rather than equality; the collection strongly retains its keys.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| Working with SplObjectStorage | SplObjectStorage models a set of objects with optional attached information. |
| Related API or alternative | WeakMap differs because it does not keep keys alive. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Membership uses identity rather than equality; the collection strongly retains its keys. |
| Confusing it with a related API | WeakMap differs because it does not keep keys alive. |
Key Takeaways
- SplObjectStorage models a set of objects with optional attached information.
- Membership uses identity rather than equality; the collection strongly retains its keys.
- WeakMap differs because it does not keep keys alive.
Use Working with SplObjectStorage when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.