关联数组与SplObjectStorage [英] Associative Array versus SplObjectStorage

查看:70
本文介绍了关联数组与SplObjectStorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究管理唯一对象集合的代码.这段代码的第一个原型使用了一个关联数组,基本上,这就是我一直这样做的方式.

I'm working on code to manage a collection of unique objects. The first prototype of this code utilises an associative array, basically as that's the way I've always done it.

但是,我也很想利用已添加到更现代版本的PHP中的功能(例如SplObjectStorage)来进行此操作,部分是作为一种学习体验,部分是因为它一定会提供优势(我已经基准测试)可以看出,在很多情况下,SplObjectStorage可以比数组更快.

However, I'm also keen on taking advantage of functionality that's been added to more modern versions of PHP such as SplObjectStorage for doing this instead, partly as a learning experience, partly because it's bound to offer advantages (benchmarks I've seen suggest that SplObjectStorage can be faster than arrays in a lot of cases).

当前的实现具有一个关联数组,我可以使用in_array检查该数组,以在向其添加新对象之前查看该数组中是否已存在对象.

The current implementation has an associative array that I check with in_array to see if an object is already in the array before adding a new object to it.

我可以用SplObjectStorage看到的最大问题是,乍一看似乎不支持键/值关联数组的行为,而只能将其视为索引数组.但是,有关PHP的较新功能的文档不符合该语言中更多已建立部分的文档标准,因此我可能会遗漏一些东西.

The big problem I can see with SplObjectStorage is that it doesn't seem (at first glance) to support key/value associative array behaviour, and can only be treated as an indexed array. However, the documentation for the newer features of PHP isn't up to the standards of the documentation of more established parts of the language and I might simply be missing something.

我可以使用SplObjectStorage代替关联数组吗?如果是这样,如何在添加新对象时定义键?更重要的是,与关联数组相比,SplObjectStorage的相对优缺点是什么?

Can I use SplObjectStorage in place of an associative array? If so, how do I define the key when adding a new object? More importantly, what are the relative advantages and disadvantages of SplObjectStorage when compared to associative arrays?

推荐答案

您不应将SplObjectStorage视为键值存储,而只能将其视为一组对象.场景中是否有东西,但是它的位置并不重要.

You shouldn't see the SplObjectStorage as a key-value store, but merely a set of objects. Something is in the set or not, but its position is not important.

SplObjectStorage中元素的键"实际上是对象的哈希.这样就无法将同一对象实例的多个副本添加到SplObjectStorage,因此您不必在添加之前检查副本是否已存在.

The "key" of an element in the SplObjectStorage is in fact the hash of the object. It makes it that it is not possible to add multiple copies of the same object instance to an SplObjectStorage, so you don't have to check if a copy already exists before adding.

但是,在PHP 5.4中有一个名为getHash()的新方法,您可以重写该方法,该方法将返回对象的哈希".从某种意义上讲,这可以返回/设置密钥,以便您可以将其存储在不同的条件下.

However, in PHP 5.4 there is a new method called getHash() which you can override that will return the "hash" of the object. This - in a sense - returns/set the key so you can allow it to store under different conditions.

SplObjectStorage的主要优点是您获得了许多用于处理不同集合( contains()removeAll()removeAllExcept())的方法.它的速度稍好一些,但是内存使用情况比普通的PHP数组差.

The main advantage of SplObjectStorage is the fact that you gain lots of methods for dealing and interacting with different sets (contains(), removeAll(), removeAllExcept() etc). Its speed is marginally better, but the memory usage is worse than normal PHP arrays.

这篇关于关联数组与SplObjectStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆