array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'de', ), 'this' => array ( 0 => 'class.splobjectstorage.php', 1 => 'SplObjectStorage', ), 'up' => array ( 0 => 'spl.datastructures.php', 1 => 'Datenstrukturen', ), 'prev' => array ( 0 => 'splfixedarray.wakeup.php', 1 => 'SplFixedArray::__wakeup', ), 'next' => array ( 0 => 'splobjectstorage.addall.php', 1 => 'SplObjectStorage::addAll', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/spl/splobjectstorage.xml', ), 'extra_header_links' => array ( 'rel' => 'alternate', 'href' => '/manual/en/feeds/class.splobjectstorage.atom', 'type' => 'application/atom+xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
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.
Beispiel #1 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));
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
bool(true) bool(true) bool(false) bool(true) bool(false) bool(false)
Beispiel #2 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]);
}
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }