3v4l.org

run code in 300+ PHP versions simultaneously
<?php class IdObjectStorage extends SplObjectStorage { public function getHash($object) { return (string) $object->id; } } $storage = new IdObjectStorage(); $array = []; $object1 = new stdClass(); $object1->id = 1; $object1->value = 'Object #1'; $storage->attach($object1); $array[$object1->id] = $object1; foreach($storage as $x) var_dump($x->value); // Object #1 foreach($array as $x) var_dump($x->value); // Object #1 $object2 = new stdClass(); $object2->id = 1; $object2->value = 'Object #2'; $array[$object2->id] = $object2; $storage->attach($object2); foreach($storage as $x) var_dump($x->value); // Object #1 foreach($array as $x) var_dump($x->value); // Object #2

preferences:
26.41 ms | 402 KiB | 5 Q