当前位置: 首页 > 后端技术 > PHP

PHP的SplObjectStorage对象存储

时间:2023-03-29 20:30:08 PHP

1。在php.net上定义定义SplObjectStorage类提供从对象到数据的映射,或者通过忽略数据,提供对象集。在需要唯一标识对象的许多情况下,这种双重用途非常有用。翻译:SplObjectStorage类提供对象到数据的映射功能,或者,通过忽略数据来提供对象集合,这在涉及唯一对象的许多情况下都很有用。SplObjectStorage类实现了一个对象存储映射表,应用于需要唯一标识多个对象的存储场景。PHP5.3.0之前只能存储对象,可以为每个对象添加对应的一条数据。SplObjectStorage类的数据存储依赖于PHP的HashTable实现。与传统的使用数组和spl_object_hash函数生成数组键相比,直接使用HashTable的实现在性能上有更大的优势。有点奇怪,在PHP手册中,SplObjectStorage类是放在datastructures目录下的。但是他的实现和观察者模式的接口是放在同一个文件中的(ext/spl/spl_observer.c)。其实他们并没有直接的关系。《深入理解php内核》2.接口描述classSplObjectStorageimplementsCountable,Iterator,Serializable,ArrayAccess{//省略,下面有详细解释和翻译}这个类实现了Countable,Iterator,Serializable,ArrayAccess四个接口,分别对应统计,迭代,序列2.1Countable这个接口只有一个方法count(),见SplObjectStorage类中对该方法的描述(源码位置在php.jar/stubs/SPL/SPL_c1.php文件第1979行,可以用phpstorm按住命令鼠标左键跳转到过去)/***Returnsthenumberofobjectsinthestorage//Returnthenumberofobjectsinthestorage*@linkhttps://php.net/manual/en/splobjectstorage.count.php*@returnint存储中的对象数。*@since5.1.0*/publicfunctioncount(){}翻译注:Returnsthenumberofobjectsinthestorage//返回存储中的对象个数2.2Iteratorinterfaceannotation用于外部迭代器或可迭代对象的接口被翻译为外部迭代器或可迭代对象的接口。该接口中有5个方法如下(对应注释中有翻译)/***Rewindtheiteratortothefirststorageelement//Returntheiteratortothefirststorageelement*@linkhttps://php.net/manual/en/spobjectstorage.rewind.php*@returnvoid*@since5.1.0*/publicfunctionrewind(){}/***如果当前迭代器返回atorentryisvalid//返回当前迭代器入口是否有效*@linkhttps://php.net/manual/en/spobjectstorage.valid.php*@returnbool如果迭代器入口有效则为真,否则为假。*@since5.1.0*/publicfunctionvalid(){}/***Returnstheindexwheretheiteratorcurrentlyis//返回当前迭代对应的索引*@linkhttps://php.net/manual/en/splobjectstorage.key.php*@returnint迭代器位置对应的索引。*@since5.1.0*/publicfunctionkey(){}/***Returnsthecurrentstorageentry//返回当前存储条目*@linkhttps://php.net/manual/en/spobjectstorage.current.php*@returnobject当前迭代器位置的对象。*@since5.1.0*/publicfunctioncurrent(){}/***移至下一个条目//移至下一个条目*@linkhttps://php.net/manual/en/spobjectstorage.next。php*@returnvoid*@since5.1.0*/publicfunctionnext(){}2.3Serializable接口注解自定义序列化接口。被翻译成自定义序列化的接口。该接口有2个方法如下(对应注释中有翻译)/***序列化存储//序列化存储*@linkhttps://php.net/manual/en/spobjectstorage.serialize.php*@returnstring表示存储的字符串。//返回表示存储的字符串*@since5.2.2*/publicfunctionserialize(){}/***从字符串表示反序列化存储//从字符串表示反序列化存储*@linkhttps://php.net/manual/en/spobjectstorage.unserialize.php*@paramstring$serialized

*存储的序列化表示。*

*@returnvoid*@since5.2.2*/publicfunctionunserialize($serialized){}2.4ArrayAccess接口注释Interface提供访问对象作为数组的翻译。就是提供一个像访问数组一样访问对象的接口。该接口中有4个方法如下(对应注释中有翻译)manual/en/spobjectstorage.offsetexists.php*@paramobject$object

*要查找的对象。*

*@returnbool如果对象存在于存储中则为真,否则为假。*@since5.3.0*/公共函数offsetExists($object){}/***将数据与存储中的对象相关联

*与数据关联的对象。*

*@parammixed$data[可选]

*与对象关联的数据。*

*@returnvoid*@since5.3.0*/publicfunctionoffsetSet($object,$data=null){}/***从存储中删除一个对象//从存储中删除一个对象*@linkhttps://php.net/manual/en/splobjectstorage.offsetunset.php*@paramobject$object

*要删除的对象。*

*@returnvoid*@since5.3.0*/publicfunctionoffsetUnset($object){}/***返回与object关联的数据//从存储中获得一个对象*@linkhttps://php.net/manual/en/splobjectstorage.offsetget.php*@paramobject$object

*要查找的对象。*

*@returnmixedst中对象之前关联的数据橙色。*@since5.3.0*/publicfunctionoffsetGet($object){}这个接口的作用用代码简单解释如下$collection=newSupor\Collection();//假设有一个Collection类和ArrayAccess已经实现接口$collection['a']=10;//我们可以像给数组赋值一样给这个对象赋值var_dump($collection['a']);//我们也可以使用取数组值获取对象属性的方法Donotuse'->'//outputint(10)3.方法描述在每个方法的注释中都有相应的翻译来解释这个方法的作用/***Addsanobjectinthestorage//AddanobjecttothestorageObject*@linkhttps://php.net/manual/en/spobjectstorage.attach.php*@paramobject$object

*的要添加的对象。*

*@parammixed$data[可选]

*与对象关联的数据。*

*@returnvoid*@since5.1.0*/publicfunctionattach($object,$data=null){}/***从存储中移除一个对象//Deleteanobjectfromstorage*@链接https://php.net/manual/en/spobjectstorage.detach.php*@paramobject$object

*要删除的对象。*

*@returnvoid*@since5.1.0*/publicfunctiondetach($object){}/***检查存储是否包含特定的object//检查存储是否包含特定对象*@linkhttps://php.net/manual/en/splobjectstorage.contains.php*@paramobject$object

*要查找的对象。*

*@returnbool如果对象在存储中则为真,否则为假。*@since5.1.0*/publicfunctioncontains($object){}/***添加来自另一个存储的所有对象//AddastorageAllobjectsin*@linkhttps://php.net/manual/en/spobjectstorage.addall.php*@paramSplObjectStorage$storage

*您要导入的存储。*

*@returnvoid*@since5.3.0*/publicfunctionaddAll($storage){}/***Removesobjectscontainedinanotherstoragefromthecurrentstorage//Deleteobjectscontainedinanotherstoragefromthecurrentstorage//删除另一个存储中包含的对象当前存储*@linkhttps://php.net/manual/en/splobjectstorage.removeall.php*@paramSplObjectStorage$storage

*包含要删除的元素的存储。*

*@returnvoid*@since5.3.0*/publicfunctionremoveAll($storage){}/***从当前存储中移除不包含在另一个存储中的对象*移除所有对象,除了t用于当前存储中包含在另一个存储中的那些*@linkhttps://php.net/manual/en/splobjectstorage.removeallexcept.php*@paramSplObjectStorage$storage

*包含要保留在当前存储。*

*@returnvoid*@since5.3.6*/publicfunctionremoveAllExcept($storage){}/***返回与当前迭代器条目关联的数据//返回当前代器相关的数据*@linkhttps://php.net/manual/en/splobjectstorage.getinfo.php*@returnmixed与当前迭代器位置关联的数据。*@since5.3.0*/publicfunctiongetInfo(){}/***设置与当前迭代器条目关联的数据//设置当代理器条目相关的数据*@linkhttps://php.net/manual/en/splobjectstorage.setinfo.php*@parammixed$data

*与当前迭代器条目关联的数据。*

*@returnvoid*@since5.3.0*/publicfunctionsetInfo($data){}/***计算t的唯一标识符hecontainedobjects//为包含的对象计算一个唯一的ID*@linkhttps://php.net/manual/en/splobjectstorage.gethash.php*@param$object

*要计算其标识符的对象。*@returnstring带有计算标识符的字符串。*@since5.4.0*/publicfunctiongetHash($object){}4.对象操作使用SplObjectStorage//假设有3个Collection对象$collection1=newSupor\Collection(['a'=>'aa','b'=>'bb']);$collection2=newSupor\Collection(['c'=>'cc','d'=>'dd']);$collection3=newSupor\Collection(['e'=>'ee','f'=>'ff']);$splStorage=newSplObjectStorage();$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->包含($collsection3);var_dump($contains3);//将指针向后移动$splStorage->next();//读取移动的key$key=$splStorage->key();var_dump($key);//删除一个object$splStorage->detach($collection3);//删除后统计$count=$splStorage->count();var_dump($count);//遍历$splStorage的所有对象//遍历前重置$splStorage指针->rewind();//当当前迭代器入口返回truewhile($splStorage->valid()){//打印当前入口var_dump($splStorage->current());//将指针移回$splStorage->next();}代码执行结果如下: