Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Standard PHP Library and Classes

The SplObjectStorage class

Reference for PHP SplObjectStorage: store objects as keys, attach data, check membership by identity, iterate, remove and combine sets, plus the PHP 8.5 deprecations.

SplObjectStorage stores objects. It works as a set (each object once) or as a map from object to any value, which an ordinary array cannot do because array keys can only be integers and strings. Objects are compared by identity, the way === compares them: two different objects with the same data are two entries.

Typical uses are observer lists, "already visited" sets, and attaching extra data to objects you do not own.

PHP
<?php
$set = new SplObjectStorage();
$a = new stdClass();
$set[$a] = 'first';
echo count($set), ' ', $set[$a];

Try it

PHP A set and a map of objects

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Attached data, set operations and the observer pattern

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
class SplObjectStorage implements Countable, Iterator, Serializable, ArrayAccess
{
    public function offsetSet(object $object, mixed $info = null): void;
    public function offsetGet(object $object): mixed;
    public function offsetExists(object $object): bool;
    public function offsetUnset(object $object): void;
}
Member Description
$storage[$obj] = $data Adds the object, or replaces its attached data. Use null data for a plain set.
$storage[$obj] Reads the attached data; throws UnexpectedValueException if the object is missing.
isset($storage[$obj]) True if the object is stored, whatever its data is.
unset($storage[$obj]) Removes the object.
addAll($other) / removeAll($other) / removeAllExcept($other) Set operations; each returns the new count.
getInfo() / setInfo($data) Data of the current element while iterating.
count() Number of stored objects.
getHash($object) Override in a subclass to define a custom identity.

Return value: count() returns an int. offsetGet() returns the attached data.

Errors / Exceptions: reading a missing object throws UnexpectedValueException. Non-objects are a TypeError.

Usage notes

  • Identity, not equality. Two objects with the same properties are different entries. Override getHash() in a subclass if you need your own notion of "same".
  • Iteration gives positions and objects. The key is a running index, the value is the object; read the attached data with $storage[$object] or getInfo().
  • The method names changed in PHP 8.5. The older attach(), detach() and contains() methods are deprecated in PHP 8.5 in favor of the array syntax (or offsetSet(), offsetUnset(), offsetExists()). New code should use the array syntax.
  • isset() is about the object. An object stored with null data still counts as present, so isset($storage[$obj]) works for sets. Unlike arrays, it does not test the attached value.
  • It keeps objects alive. Stored objects are not garbage collected until removed. For caches that should not extend lifetimes, use WeakMap.
  • Do not remove while iterating with foreach over the same storage; collect objects first, then remove them.
  • Cloning copies the set; the contained objects are not cloned.