(PHP 5 >= 5.3.0, PHP 7)
Examples:
SplObjectStorage as a set
<?php // As an object set $s = new SplObjectStorage(); $o1 = new StdClass; $o2 = new StdClass; $o3 = new StdClass; $s->attach($o1); $s->attach($o2); var_dump($s->contains($o1)); var_dump($s->contains($o2)); var_dump($s->contains($o3)); $s->detach($o2); var_dump($s->contains($o1)); var_dump($s->contains($o2)); var_dump($s->contains($o3)); ?>
The above example will output:
bool(true) bool(true) bool(false) bool(true) bool(false) bool(false)
SplObjectStorage as a map
<?php // As a map from objects to data $s = new SplObjectStorage(); $o1 = new StdClass; $o2 = new StdClass; $o3 = new StdClass; $s[$o1] = "data for object 1"; $s[$o2] = array(1,2,3); if (isset($s[$o2])) { var_dump($s[$o2]); } ?>
The above example will output:
array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }