Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

SplObjectStorage

Associate data with object identities and keep a strong reference to each stored key.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

new SplObjectStorage()

Associate data with object identities and keep a strong reference to each stored key. Distinct objects with equal properties are separate entries. Reassigning the same object replaces its associated data without increasing the count. Unset an entry when the storage should release it.

$store=new SplObjectStorage(); $a=(object)["id"=>1]; $b=(object)["id"=>1];
$store[$a]="old"; $store[$a]="new"; $store[$b]="other"; $before=count($store); unset($store[$b]);
echo json_encode([$before,count($store),$store[$a],isset($store[$b])]);
[2,1,"new",false]

Examples

<?php
$store=new SplObjectStorage(); $a=(object)["id"=>1]; $b=(object)["id"=>1];
$store[$a]="old"; $store[$a]="new"; $store[$b]="other"; $before=count($store); unset($store[$b]);
echo json_encode([$before,count($store),$store[$a],isset($store[$b])]);

Result

[2,1,"new",false]

Sources