首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

SplObjectStorage (class)

Introduction

(PHP 5 >= 5.3.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.

Class synopsis

SplObjectStorage implements Countable , Iterator , Serializable , ArrayAccess {

/* Methods */

代码语言:javascript
复制
public void addAll ( SplObjectStorage $storage )
代码语言:javascript
复制
public void attach ( object $object [, mixed $data = NULL ] )
代码语言:javascript
复制
public bool contains ( object $object )
代码语言:javascript
复制
public int count ( void )
代码语言:javascript
复制
public object current ( void )
代码语言:javascript
复制
public void detach ( object $object )
代码语言:javascript
复制
public string getHash ( object $object )
代码语言:javascript
复制
public mixed getInfo ( void )
代码语言:javascript
复制
public int key ( void )
代码语言:javascript
复制
public void next ( void )
代码语言:javascript
复制
public bool offsetExists ( object $object )
代码语言:javascript
复制
public mixed offsetGet ( object $object )
代码语言:javascript
复制
public void offsetSet ( object $object [, mixed $data = NULL ] )
代码语言:javascript
复制
public void offsetUnset ( object $object )
代码语言:javascript
复制
public void removeAll ( SplObjectStorage $storage )
代码语言:javascript
复制
public void removeAllExcept ( SplObjectStorage $storage )
代码语言:javascript
复制
public void rewind ( void )
代码语言:javascript
复制
public string serialize ( void )
代码语言:javascript
复制
public void setInfo ( mixed $data )
代码语言:javascript
复制
public void unserialize ( string $serialized )
代码语言:javascript
复制
public bool valid ( void )

}

Examples

Example #1 SplObjectStorage as a set

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)

Example #2 SplObjectStorage as a map

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

Table of Contents

  • SplObjectStorage::addAll — Adds all objects from another storage
  • SplObjectStorage::attach — Adds an object in the storage
  • SplObjectStorage::contains — Checks if the storage contains a specific object
  • SplObjectStorage::count — Returns the number of objects in the storage
  • SplObjectStorage::current — Returns the current storage entry
  • SplObjectStorage::detach — Removes an object from the storage
  • SplObjectStorage::getHash — Calculate a unique identifier for the contained objects
  • SplObjectStorage::getInfo — Returns the data associated with the current iterator entry
  • SplObjectStorage::key — Returns the index at which the iterator currently is
  • SplObjectStorage::offsetExists — Checks whether an object exists in the storage
  • SplObjectStorage::offsetGet — Returns the data associated with an object
  • SplObjectStorage::offsetSet — Associates data to an object in the storage
  • SplObjectStorage::offsetUnset — Removes an object from the storage
  • SplObjectStorage::removeAll — Removes objects contained in another storage from the current storage
  • SplObjectStorage::removeAllExcept — Removes all objects except for those contained in another storage from the current storage
  • SplObjectStorage::rewind — Rewind the iterator to the first storage element
  • SplObjectStorage::serialize — Serializes the storage
  • SplObjectStorage::setInfo — Sets the data associated with the current iterator entry
  • SplObjectStorage::unserialize — Unserializes a storage from its string representation
  • SplObjectStorage::valid — Returns if the current iterator entry is valid

← SplFixedArray::__wakeup

SplObjectStorage::addAll →

代码语言:txt
复制
 ? 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com