Php/docs/class.splobjectstorage

来自菜鸟教程
跳转至:导航、​搜索

(PHP 5 >= 5.1.0, PHP 7)

简介

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.


类摘要


SplObjectStorage implements Countable , Iterator , Serializable , ArrayAccess {

/* 方法 */

public addAll ( SplObjectStorage $storage ) : void

public attach ( object $object [, mixed $data = null ] ) : void

public contains ( object $object ) : bool

public count ( ) : int

public current ( ) : object

public detach ( object $object ) : void

public getHash ( object $object ) : string

public getInfo ( ) : mixed

public key ( ) : int

public next ( ) : void

public offsetExists ( object $object ) : bool

public offsetGet ( object $object ) : mixed

public offsetSet ( object $object [, mixed $data = null ] ) : void

public offsetUnset ( object $object ) : void

public removeAll ( SplObjectStorage $storage ) : void

public removeAllExcept ( SplObjectStorage $storage ) : void

public rewind ( ) : void

public serialize ( ) : string

public setInfo ( mixed $data ) : void

public unserialize ( string $serialized ) : void

public valid ( ) : bool

}

范例

Example #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));?>

以上例程会输出:


bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)

Example #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]);}?>

以上例程会输出:


array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

Table of Contents