php.net上的定义 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类提供从对象到数据映射功能,或者,经过忽视数据来提供对象集合,在不少涉及须要惟一对象的许多状况下,这两点是十分有用的。
php
SplObjectStorage类实现了对象存储映射表,应用于须要惟一标识多个对象的存储场景。 在PHP5.3.0以前仅能存储对象,以后能够针对每一个对象添加一条对应的数据。 SplObjectStorage类的数据存储依赖于PHP的HashTable实现,与传统的使用数组和spl_object_hash函数生成数组key相比, 其直接使用HashTable的实如今性能上有较大的优点。 有一些奇怪的是,在PHP手册中,SplObjectStorage类放在数据结构目录下。 可是他的实现和观察者模式的接口放在同一个文件(ext/spl/spl_observer.c)。 实际上他们并无直接的关系。《深刻理解php内核》
class SplObjectStorage implements Countable, Iterator, Serializable, ArrayAccess { //省略,下边详细解释以及翻译 }
此类实现了 Countable, Iterator, Serializable, ArrayAccess 四个接口,分别对应统计,迭代,序列化和数组访问,四个接口分别说明以下数组
此接口中只有一方法count()
,看SplObjectStorage
类中此方法的说明(源码位置在php.jar/stubs/SPL/SPL_c1.php文件的1979行
,能够用phpstorm按住command鼠标左键跳转过去)数据结构
/** * Returns the number of objects in the storage //返回存储中的对象数量 * @link https://php.net/manual/en/splobjectstorage.count.php * @return int The number of objects in the storage. * @since 5.1.0 */ public function count () {}
翻译注释:Returns the number of objects in the storage //返回存储中的对象数量phpstorm
接口注释Interface for external iterators or objects that can be iterated
的翻译为外部迭代器或能够迭代的对象的接口
,此接口中有5个方法分别以下(对应注释中有翻译)ide
/** * Rewind the iterator to the first storage element //将迭代器回到第一个存储的元素 * @link https://php.net/manual/en/splobjectstorage.rewind.php * @return void * @since 5.1.0 */ public function rewind () {} /** * Returns if the current iterator entry is valid //返回当前迭代器条目是否有效 * @link https://php.net/manual/en/splobjectstorage.valid.php * @return bool true if the iterator entry is valid, false otherwise. * @since 5.1.0 */ public function valid () {} /** * Returns the index at which the iterator currently is//返回当前迭代对应的索引 * @link https://php.net/manual/en/splobjectstorage.key.php * @return int The index corresponding to the position of the iterator. * @since 5.1.0 */ public function key () {} /** * Returns the current storage entry //返回当前存储的条目 * @link https://php.net/manual/en/splobjectstorage.current.php * @return object The object at the current iterator position. * @since 5.1.0 */ public function current () {} /** * Move to the next entry //移到下一个条目 * @link https://php.net/manual/en/splobjectstorage.next.php * @return void * @since 5.1.0 */ public function next () {}
接口注释Interface for customized serializing.
的翻译为用于自定义序列化的接口
,此接口中有2个方法分别以下(对应注释中有翻译)函数
/** * Serializes the storage //序列化存储 * @link https://php.net/manual/en/splobjectstorage.serialize.php * @return string A string representing the storage. //返回表示存储的字符串 * @since 5.2.2 */ public function serialize () {} /** * Unserializes a storage from its string representation //从一个字符串表示中对存储反序列化 * @link https://php.net/manual/en/splobjectstorage.unserialize.php * @param string $serialized <p> * The serialized representation of a storage. * </p> * @return void * @since 5.2.2 */ public function unserialize ($serialized) {}
接口注释Interface to provide accessing objects as arrays.
的翻译为提供像访问数组同样访问对象的接口
,此接口中有4个方法分别以下(对应注释中有翻译)性能
/** * Checks whether an object exists in the storage //检查存储中是否存在找个对象 * @link https://php.net/manual/en/splobjectstorage.offsetexists.php * @param object $object <p> * The object to look for. * </p> * @return bool true if the object exists in the storage, * and false otherwise. * @since 5.3.0 */ public function offsetExists ($object) {} /** * Associates data to an object in the storage //给存储中的对象赋值 * @link https://php.net/manual/en/splobjectstorage.offsetset.php * @param object $object <p> * The object to associate data with. * </p> * @param mixed $data [optional] <p> * The data to associate with the object. * </p> * @return void * @since 5.3.0 */ public function offsetSet ($object, $data = null) {} /** * Removes an object from the storage //从存储中删除一个对象 * @link https://php.net/manual/en/splobjectstorage.offsetunset.php * @param object $object <p> * The object to remove. * </p> * @return void * @since 5.3.0 */ public function offsetUnset ($object) {} /** * Returns the data associated with an <type>object</type> //从存储中得到一个对象 * @link https://php.net/manual/en/splobjectstorage.offsetget.php * @param object $object <p> * The object to look for. * </p> * @return mixed The data previously associated with the object in the storage. * @since 5.3.0 */ public function offsetGet ($object) {}
此接口的功能用代码简单说明以下spa
$collection = new Supor\Collection();//假设有一Collection类,而且已经实现了ArrayAccess 接口 $collection['a'] = 10;//咱们能够像给数组赋值同样给此对象赋值 var_dump($collection['a']);//也可使用取数组值的方法取得对象的属性 而不用 '->' //输出 int(10)
在每一个方法的注释中有对应翻译,来讲明这个方法的做用.net
/** * Adds an object in the storage //向存储中添加一个对象 * @link https://php.net/manual/en/splobjectstorage.attach.php * @param object $object <p> * The object to add. * </p> * @param mixed $data [optional] <p> * The data to associate with the object. * </p> * @return void * @since 5.1.0 */ public function attach ($object, $data = null) {} /** * Removes an object from the storage //从存储中删除一个对象 * @link https://php.net/manual/en/splobjectstorage.detach.php * @param object $object <p> * The object to remove. * </p> * @return void * @since 5.1.0 */ public function detach ($object) {} /** * Checks if the storage contains a specific object //检查存储中是否包含特定的对象 * @link https://php.net/manual/en/splobjectstorage.contains.php * @param object $object <p> * The object to look for. * </p> * @return bool true if the object is in the storage, false otherwise. * @since 5.1.0 */ public function contains ($object) {} /** * Adds all objects from another storage //添加一个存储中全部对象 * @link https://php.net/manual/en/splobjectstorage.addall.php * @param SplObjectStorage $storage <p> * The storage you want to import. * </p> * @return void * @since 5.3.0 */ public function addAll ($storage) {} /** * Removes objects contained in another storage from the current storage //从当前存储中删除另外一个存储中包含的对象 * @link https://php.net/manual/en/splobjectstorage.removeall.php * @param SplObjectStorage $storage <p> * The storage containing the elements to remove. * </p> * @return void * @since 5.3.0 */ public function removeAll ($storage) {} /** *从当前存储中删除另外一个存储中不包含的对象 * Removes all objects except for those contained in another storage from the current storage * @link https://php.net/manual/en/splobjectstorage.removeallexcept.php * @param SplObjectStorage $storage <p> * The storage containing the elements to retain in the current storage. * </p> * @return void * @since 5.3.6 */ public function removeAllExcept ($storage) {} /** * Returns the data associated with the current iterator entry //返回当前迭代器条目相关的数据 * @link https://php.net/manual/en/splobjectstorage.getinfo.php * @return mixed The data associated with the current iterator position. * @since 5.3.0 */ public function getInfo () {} /** * Sets the data associated with the current iterator entry//设置当前迭代器条目相关的数据 * @link https://php.net/manual/en/splobjectstorage.setinfo.php * @param mixed $data <p> * The data to associate with the current iterator entry. * </p> * @return void * @since 5.3.0 */ public function setInfo ($data) {} /** * Calculate a unique identifier for the contained objects //给包含的对象计算一个惟一ID * @link https://php.net/manual/en/splobjectstorage.gethash.php * @param $object <p> * object whose identifier is to be calculated. * @return string A string with the calculated identifier. * @since 5.4.0 */ public function getHash($object) {}
SplObjectStorage的对象操做翻译
//假设有三个Collection对象 $collection1 = new Supor\Collection(['a' => 'aa', 'b' => 'bb']); $collection2 = new Supor\Collection(['c' => 'cc', 'd' => 'dd']); $collection3 = new Supor\Collection(['e' => 'ee', 'f' => 'ff']); $splStorage = new SplObjectStorage(); $splStorage->attach($collection1); //传入相同的对象会被替代 $splStorage->attach($collection1); $splStorage->attach($collection2); $splStorage->attach($collection3); //统计$splStorage中有多少个对象 $count = $splStorage->count(); var_dump($count); //获得某一对象的哈希值 $hash1 = $splStorage->getHash($collection1); var_dump($hash1); //检查存储中是否包含$collection3 $contains3 = $splStorage->contains($collection3); var_dump($contains3); //将指针后移 $splStorage->next(); //读取移动后的key $key = $splStorage->key(); var_dump($key); //删除某个对象 $splStorage->detach($collection3); //统计删除后的数量 $count = $splStorage->count(); var_dump($count); //遍历$splStorage全部对象 //遍历前先重置一下指针 $splStorage->rewind(); //当当前迭代器条目返回真时 while ($splStorage->valid()) { //打印当前条目 var_dump($splStorage->current()); //指针后移 $splStorage->next(); }
代码执行结果以下: