ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# SplObjectStorage(映射【对象存储】) # [SplObjectStorage:对象存储](https://www.php.net/manual/zh/class.splobjectstorage.php#class.splobjectstorage) > 一般用来存储对象 > splObjectStorage类提供从对象到数据或忽略数据的对象集的映射。这种双重目的在许多情况下都是有用的,包括需要唯一地标识对象。 > 用来存储一组对象的,特别是当你需要唯一标识对象的时候 ``` <pre class="calibre10">``` SplObjectStorage implements <span class="token4">Countable</span> <span class="token3">,</span> Iterator <span class="token3">,</span> Serializable <span class="token3">,</span> ArrayAccess <span class="token3">{</span> <span class="token">/* 方法 */</span> <span class="token">//Countable</span> public count <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> int <span class="token">//返回存储中的对象数量</span> <span class="token">//Iterator </span> public current <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> object <span class="token">//返回当前存储的条目</span> public key <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> int <span class="token">//返回当前迭代对应的索引</span> public next <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//移到下一条目</span> public rewind <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//将迭代器回到第一个存储的元素</span> public valid <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//返回当前迭代器条目是否有效</span> <span class="token">//Serializable </span> public serialize <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> string <span class="token">//序列化存储 返回表示存储的字符串 对继承他的子类使用serialize时触发</span> public unserialize <span class="token3">(</span> string $serialized <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//反序列化存储 对继承他的子类使用unserialize 时触发</span> <span class="token">//ArrayAccess </span> public offsetExists <span class="token3">(</span> object $object <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//isset(obj)触发 判断存储对象中是否含有指定的对象</span> public offsetGet <span class="token3">(</span> object $object <span class="token3">)</span> <span class="token3">:</span> mixed <span class="token">//从存储中获得一个对象</span> public offsetSet <span class="token3">(</span> object $object <span class="token3">[</span><span class="token3">,</span> mixed $data <span class="token1">=</span> NULL <span class="token3">]</span> <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//给存储中的对象赋值</span> public offsetUnset <span class="token3">(</span> object $object <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//从存储中删除一个对象</span> public addAll <span class="token3">(</span> SplObjectStorage $storage <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//添加一个指定存储中所有的对象</span> public attach <span class="token3">(</span> object $object <span class="token3">[</span><span class="token3">,</span> mixed $data <span class="token1">=</span> NULL <span class="token3">]</span> <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//象存储的对象中添加一个对象</span> public contains <span class="token3">(</span> object $object <span class="token3">)</span> <span class="token3">:</span> bool <span class="token">//检查存储中是否包含特定的对象</span> public detach <span class="token3">(</span> object $object <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//从存储对象中删除一个指定的对象</span> public getHash <span class="token3">(</span> object $object <span class="token3">)</span> <span class="token3">:</span> string <span class="token">//给包含的对象计算一个唯一ID</span> public getInfo <span class="token3">(</span> void <span class="token3">)</span> <span class="token3">:</span> mixed <span class="token">//返回当前迭代器条目相关的数据</span> public removeAll <span class="token3">(</span> SplObjectStorage $storage <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//从当前存储中删除另一个存储中包含的对象</span> public removeAllExcept <span class="token3">(</span> SplObjectStorage $storage <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//从当前存储中删除另一个存储中不包含的对象</span> public setInfo <span class="token3">(</span> mixed $data <span class="token3">)</span> <span class="token3">:</span> void <span class="token">//设置当前迭代器条目相关的数据</span> <span class="token3">}</span> ``` ``` ## **例子** ``` <pre class="calibre10">``` class <span class="token4">Person</span> <span class="token3">{</span> public $name<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>string $name<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>name <span class="token1">=</span> $name<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//实例化对象</span> $zhangsan <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Person</span><span class="token3">(</span><span class="token2">'张三'</span><span class="token3">)</span><span class="token3">;</span> $lisi <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Person</span><span class="token3">(</span><span class="token2">'李四'</span><span class="token3">)</span><span class="token3">;</span> $wangwu <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Person</span><span class="token3">(</span><span class="token2">'王五'</span><span class="token3">)</span><span class="token3">;</span> $zhaoliu <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Person</span><span class="token3">(</span><span class="token2">'赵六'</span><span class="token3">)</span><span class="token3">;</span> $wangmazi <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Person</span><span class="token3">(</span><span class="token2">'王麻子'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//实例化对象存储</span> $container <span class="token1">=</span> <span class="token5">new</span> <span class="token4">SplObjectStorage</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//往存储空间写入对象</span> $container<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$zhangsan<span class="token3">)</span><span class="token3">;</span> $container<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$lisi<span class="token3">)</span><span class="token3">;</span> $container<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$wangwu<span class="token3">)</span><span class="token3">;</span> $container<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$zhaoliu<span class="token3">)</span><span class="token3">;</span> <span class="token">//统计存储空间里面的对象的数量</span> echo <span class="token2">"存储对象数量"</span> <span class="token3">.</span> $container<span class="token1">-</span><span class="token1">></span><span class="token4">count</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//4</span> echo <span class="token2">"<br/>"</span><span class="token3">;</span> <span class="token">//判断指定的对象是否在存储空间中</span> echo <span class="token2">"是否包含指定对象:"</span><span class="token3">;</span> <span class="token4">var_dump</span><span class="token3">(</span>$container<span class="token1">-</span><span class="token1">></span><span class="token4">contains</span><span class="token3">(</span>$lisi<span class="token3">)</span><span class="token3">)</span><span class="token3">;</span><span class="token">//true</span> <span class="token4">var_dump</span><span class="token3">(</span>$container<span class="token1">-</span><span class="token1">></span><span class="token4">contains</span><span class="token3">(</span>$wangmazi<span class="token3">)</span><span class="token3">)</span><span class="token3">;</span><span class="token">//false</span> echo <span class="token2">"<br/>"</span><span class="token3">;</span> <span class="token">//从存储中删除指定对象</span> echo <span class="token2">"删除指定对象"</span><span class="token3">;</span> $container<span class="token1">-</span><span class="token1">></span><span class="token4">detach</span><span class="token3">(</span>$wangwu<span class="token3">)</span><span class="token3">;</span> <span class="token">//查看当前指针所在的索引位置</span> echo <span class="token2">"指针索引"</span> <span class="token3">.</span> $container<span class="token1">-</span><span class="token1">></span><span class="token4">key</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//0</span> echo <span class="token2">"<br/>"</span><span class="token3">;</span> <span class="token">//重置存储空间指针</span> $container<span class="token1">-</span><span class="token1">></span><span class="token4">rewind</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//校验当前指针是否有效</span> <span class="token4">var_dump</span><span class="token3">(</span>$container<span class="token1">-</span><span class="token1">></span><span class="token4">valid</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span><span class="token">//true</span> <span class="token">//遍历存储空间</span> echo <span class="token2">""</span><span class="token3">;</span> <span class="token5">while</span> <span class="token3">(</span>$container<span class="token1">-</span><span class="token1">></span><span class="token4">valid</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">//获取当前指针位置的对象</span> <span class="token4">var_dump</span><span class="token3">(</span>$container<span class="token1">-</span><span class="token1">></span><span class="token4">current</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//存储空间指针下移</span> $container<span class="token1">-</span><span class="token1">></span><span class="token4">next</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> ``` ``` ## **例子2** ``` <pre class="calibre17">``` <span class="token1">/</span>假设有三个Collection对象 $collection1 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Supor<span class="token3">\</span>Collection</span><span class="token3">(</span><span class="token3">[</span><span class="token2">'a'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'aa'</span><span class="token3">,</span> <span class="token2">'b'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'bb'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> $collection2 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Supor<span class="token3">\</span>Collection</span><span class="token3">(</span><span class="token3">[</span><span class="token2">'c'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'cc'</span><span class="token3">,</span> <span class="token2">'d'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'dd'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> $collection3 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Supor<span class="token3">\</span>Collection</span><span class="token3">(</span><span class="token3">[</span><span class="token2">'e'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'ee'</span><span class="token3">,</span> <span class="token2">'f'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'ff'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> $splStorage <span class="token1">=</span> <span class="token5">new</span> <span class="token4">SplObjectStorage</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$collection1<span class="token3">)</span><span class="token3">;</span> <span class="token">//传入相同的对象会被替代</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$collection1<span class="token3">)</span><span class="token3">;</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$collection2<span class="token3">)</span><span class="token3">;</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$collection3<span class="token3">)</span><span class="token3">;</span> <span class="token">//统计$splStorage中有多少个对象</span> $count <span class="token1">=</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">count</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token4">var_dump</span><span class="token3">(</span>$count<span class="token3">)</span><span class="token3">;</span> <span class="token">//得到某一对象的哈希值</span> $hash1 <span class="token1">=</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">getHash</span><span class="token3">(</span>$collection1<span class="token3">)</span><span class="token3">;</span> <span class="token4">var_dump</span><span class="token3">(</span>$hash1<span class="token3">)</span><span class="token3">;</span> <span class="token">//检查存储中是否包含$collection3</span> $contains3 <span class="token1">=</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">contains</span><span class="token3">(</span>$collection3<span class="token3">)</span><span class="token3">;</span> <span class="token4">var_dump</span><span class="token3">(</span>$contains3<span class="token3">)</span><span class="token3">;</span> <span class="token">//将指针后移</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">next</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//读取移动后的key</span> $key <span class="token1">=</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">key</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token4">var_dump</span><span class="token3">(</span>$key<span class="token3">)</span><span class="token3">;</span> <span class="token">//删除某个对象</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">detach</span><span class="token3">(</span>$collection3<span class="token3">)</span><span class="token3">;</span> <span class="token">//统计删除后的数量</span> $count <span class="token1">=</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">count</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token4">var_dump</span><span class="token3">(</span>$count<span class="token3">)</span><span class="token3">;</span> <span class="token">//遍历$splStorage所有对象</span> <span class="token">//遍历前先重置一下指针</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">rewind</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//当当前迭代器条目返回真时</span> <span class="token5">while</span> <span class="token3">(</span>$splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">valid</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">//打印当前条目</span> <span class="token4">var_dump</span><span class="token3">(</span>$splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">current</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//指针后移</span> $splStorage<span class="token1">-</span><span class="token1">></span><span class="token4">next</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> ``` ```