PHP Portal » PHP Handbuch » The SplObjectStorage class

Werbung

The SplObjectStorage class


Einführung

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.

Klassenbeschreibung

SplObjectStorage
SplObjectStorage implements Countable , Iterator , Traversable , Serializable , ArrayAccess {
/* Methods */
public void addAll ( SplObjectStorage $storage )
public void attach ( object $object [, mixed $data = null ] )
public boolean contains ( object $object )
public int count ( void )
public object current ( void )
public void detach ( object $object )
public mixed getInfo ( void )
public int key ( void )
public void next ( void )
public boolean offsetExists ( object $object )
public mixed offsetGet ( object $object )
public void offsetSet ( object $object , mixed $info )
public void offsetUnset ( object $object )
public void removeAll ( SplObjectStorage $storage )
public void rewind ( void )
public string serialize ( void )
public void setInfo ( mixed $data )
public void unserialize ( string $serialized )
public boolean valid ( void )
}

Beispiele

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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)

PHP Code
1
2
3
4
5
6
7
8
9
10
11
// 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)
}

Inhaltsverzeichnis