Content · Web Development · PHP

Working with SplObjectStorage

SplObjectStorage models a set of objects with optional attached information. Membership uses identity rather than equality; the collection strongly retains its keys.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

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


ApproachDifference
Working with SplObjectStorageSplObjectStorage models a set of objects with optional attached information.
Related API or alternativeWeakMap differs because it does not keep keys alive.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsMembership uses identity rather than equality; the collection strongly retains its keys.
Confusing it with a related APIWeakMap 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.

Share this resourceSend this page to someone who may find it useful.