Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

SplObjectStorage::getHash() May No Longer Mutate Storage

A custom getHash() implementation in an SplObjectStorage subclass used to be free to mutate any SplObjectStorage instance, including a different one than the one currently being hashed, while computing the hash. In PHP 8.6, attempting such a mutation from inside getHash() throws an Error, because SplObjectStorage internals are not reentrant during hashing.

PHP code

<?php

class WatchfulStorage extends SplObjectStorage {
    public SplObjectStorage $sideStorage;
    public function getHash(object $object): string {
        $this->sideStorage->offsetSet(new stdClass());
        return spl_object_hash($object);
    }
}

$watch = new WatchfulStorage();
$watch->sideStorage = new SplObjectStorage();

try {
    $watch->offsetSet(new stdClass());
    echo "offsetSet succeeded, sideStorage count=".count($watch->sideStorage)."\n";
} catch (\Error $e) {
    echo "Error: ".$e->getMessage()."\n";
}

?>

Before

offsetSet succeeded, sideStorage count=1

After

Error: Modification of SplObjectStorage during getHash() is prohibited

PHP version change

This behavior changed in 8.6.

See Also

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat