PHP — SplObjectStorage Class

Vishwa Chikate
4 min readMay 7, 2022

--

I was looking for a way to store objects as keys in an array, some use case wanted me to do so, after searching for it i came across this class which provides us with similar functionality. SPL namespace in PHP provides with a collection of common data structure classes viz. LinkedList, Stack, Queues, Heap, and MaxHeap.

The SplObjectStorage class provides a map from objects to data or, by ignoring data, an object set. This dual purpose can be useful in many cases involving the need to uniquely identify objects.

Let us check the class in details and also all the methods provided by it.

addAll():

Add all entries from one SplObjectStorage into the current.

// Signature of the method
addAll(SplObjectStorage $storage): int
$sos = new \SplObjectStorage();
$stdClass = new \StdClass;
$sos->attach($stdClass);
$sosClone = new \SplObjectStorage();
$sosClone->addAll($sos);

attach():

Add some object inside the storage and associate some data with it.

// Signature of the method
attach(object $object, mixed $info = null): void
$sos = new \SplObjectStorage();
$stdClass = new \StdClass;
$exception = new \Exception();
$sos->attach($stdClass);
$sos->attach($exception, 'The Exception Class Object');

contains():

Checks if the storage contains the object provided.

// Signature of the method
contains(object $object): bool
$sos = new \SplObjectStorage();
$stdClass = new StdClass;
$sos->attach($stdClass);
var_dump($sos->contains($stdClass));
// bool(true)

count():

Returns the number of object’s added to the storage. This method is part of the \Countable interface implemented by the \SplObjectStorage class.

// Signature of the method
count(int $mode = COUNT_NORMAL): int
$sos = new \SplObjectStorage();
$stdClass = new StdClass;
$sos->attach($stdClass);
var_dump($sos->count();
// int(1)

current(), key(), valid(), next(), rewind():

These methods are provided by the \Iterator interface and implemented by the \SplObjectStorage class.

  1. current() — Current element pointed by the \Iterator class, points to the first element added to the storage.
  2. key() — Key of the current element.
  3. valid() — Check if current position is valid.
  4. next() — Moves the current position to the next element.
  5. rewind() — Moves the current position to the first element in the storage.
// Signature of the methodscurrent(): mixed
key(): mixed
next(): void
rewind(): void
valid(): bool
$sos = new SplObjectStorage();
$stdClass1 = new StdClass;
$exception = new \Exception();
$sos->attach($stdClass1);
$sos->attach($exception, 'The Exception Class Object');
var_dump($sos->current());
// object(stdClass)#2 (0) {}
var_dump($sos->key());
// int(0)
$sos->next();
var_dump($sos->current());
// object(Exception)#3 (7) {}
$sos->next();
$sos->rewind();
var_dump($sos->current());
// object(stdClass)#2 (0) {}
var_dump($sos->valid());
// bool(true)

detach():

Remove the object from the storage.

// Signature of the method
detach(object $object): void
$sos = new SplObjectStorage();
$stdClass1 = new StdClass;
$exception = new \Exception();
$sos->attach($stdClass1);
$sos->attach($exception, 'The Exception Class Object');
$sos->detach($stdClass1);
var_dump($sos->current());
// object(Exception)#3 (7) {}

getHash(), getInfo(), setInfo():

  1. getHash() — Returns a unique identifier for the passed object. This method is similar to “ spl_object_hash() ”.
  2. getInfo() — Return the data associated with the current pointed by the \Iterator class.
  3. setInfo() — Sets the data associated with the current iterator entry. Better to be used within a loop.
// Signature of the method
getHash(object $object): string
getInfo(): mixed
setInfo(mixed $info): void
$sos = new SplObjectStorage();
$stdClass1 = new StdClass;
$sos->attach($stdClass1, 'The StdClass Object');var_dump($sos->getHash($stdClass1));
// string(32) "000000005145135c0000000054d43f40"
var_dump($sos->getInfo());
// string(19) "The StdClass Object"
$sos->setInfo('The nes StdClass Object');
var_dump($sos->getInfo());
// string(19) "The new StdClass Object"

offsetExists(), offsetGet(), offsetSet(), offsetUnset()

These methods are provided by the \ArrayAccess interface.

  1. offsetExists — Check if an offset is valid. This method is similar to the contains method.
  2. offsetGet — Return the value at the given offset. This method is similar to the getInfo method.
  3. offsetSet — Assign or Update a value to the specified offset.
  4. offsetUnset — Delete the entry at the given offset.
// Signature of the methodsoffsetExists(mixed $offset): bool
offsetGet(mixed $offset): mixed
offsetSet(mixed $offset, mixed $value): void
offsetUnset(mixed $offset): void
$sos = new SplObjectStorage();
$stdClass1 = new StdClass;
$exception = new \Exception();

$sos->attach($stdClass1, 'The StdClass Object');
$sos->attach($exception, 'The Exception Class Object');
var_dump($sos->offsetExists($stdClass1));
// bool(true)
$sos->offsetSet($stdClass1, 'The new StdClass Object');var_dump($sos->offsetGet($stdClass1));
// string(19) "The new StdClass Object"
$sos->offsetUnset($stdClass1);
var_dump($sos->current());
// object(Exception)#3 (7) {}

removeAll(), removeAllExcept():

  1. removeAll() — Remove object from another storage which were added from the current one. This method can be seen as the opposite of the addAll() method described at top.
  2. removeAllExcept() — Removes all objects except for those contained in another storage from the current storage
// Signature of the method
removeAll(SplObjectStorage $storage): int
removeAllExcept(SplObjectStorage $storage): int
$sos = new SplObjectStorage();

$stdClass1 = new StdClass;
$exception = new \Exception();

$sos->attach($stdClass1, 'The StdClass Object');
$sos->attach($exception, 'The Exception Class Object');

$sosTwo = new SplObjectStorage();
$sosTwo->attach($stdClass1);

$sosTwo->removeAll($sos);
var_dump($sosTwo->count());
// int(0)
$sosTwo->removeAllExcept($sos);
var_dump($sosTwo->count());
// int(1)

There are many useful classes defined under the Spl namespace, please do check the \SplStack(), \SplQueue(), \SplHeap() etc Classes for more info.

--

--