SplObjectStorage 不适用于 String,该怎么办?
有人建议使用 SplObjectStorage 来跟踪一组独特的事物。很好,但它不适用于字符串。错误提示“ SplObjectStorage::attach() 期望参数 1 为对象,第 59 行 fback.php 中给出的字符串”
有什么想法吗?
Someone has suggested to e to use SplObjectStorage to keep track of a set of unique things. Great, except it doesn't work with strings. An error says " SplObjectStorage::attach() expects parameter 1 to be object, string given in fback.php on line 59"
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SplObjectStorage
顾名思义:存储类用于存储对象。与其他一些编程语言相比,字符串
在 PHP 中不是对象,它们是字符串;-)。因此,将字符串存储在SplObjectStorage
中是没有意义的 - 即使您将字符串包装在stdClass
类的对象中。存储唯一字符串集合的最佳方法是使用数组(作为哈希表),以字符串作为键和值(如 伊恩·塞尔比)。
不过,您可以将此功能包装到自定义类中:
顺便说一句,您可以模拟 PHP
SplObjectStorage
的行为。 5.3.0 并更好地了解它的作用。SplObjectStorage
为每个实例存储唯一的哈希值(例如 <代码>spl_object_hash()) 到能够识别对象实例。正如我上面所说:字符串根本不是对象,因此它没有实例哈希。可以通过比较字符串值来检查字符串的唯一性 - 当两个字符串包含相同的字节集时,它们相等。
The
SplObjectStorage
is what its name says: a storage class for storing objects. In contrast to some other programming languagesstrings
are not objects in PHP, they are, well, strings ;-). It therefore makes no sense to store strings in aSplObjectStorage
- even if you wrap your strings in an object of classstdClass
.The best way to store a collection of unique strings si to use arrays (as hashtables) with the string as the key as well as the value (as suggested by Ian Selby).
You could however wrap this functionality into a custom class:
By the way you san simulate the behavior of
SplObjectStorage
for PHP < 5.3.0 and to get a better understanding of what it does.SplObjectStorage
stores a unique hash for each instance (likespl_object_hash()
) tobe able to identify object instances. As I said above: a string is not an object at all, it therefore does not have an instance hash. A string's uniqueness can be checked by comparing the string values - two strings are equal when they contain the same set of bytes.
它是一个对象存储。字符串是一个标量。因此,请使用 SplString。
It's an Object Storage. A string is a scalar. So use SplString.
将字符串包装在 stdClass 中?
但是,即使字符串相同,以这种方式创建的每个对象仍然是唯一的。
如果您需要担心重复的字符串,也许您应该使用散列(关联数组)来跟踪它们?
Wrap the string in a stdClass?
However, every object created this way would still be unique, even if the strings are identical.
If you need to worry about duplicate strings, perhaps you should be using a hash (associative array) to track them instead?
如果你想确保数组中字符串的唯一性,你可以做一些事情......首先是简单地使用 array_unique()。那,或者您可以创建一个以字符串作为键和值的关联数组:
如果您想对此进行面向对象,您可以执行以下操作:
然后,在代码中您可以执行以下操作:
并迭代同样的方式:
SplObjectStorage 用于跟踪对象的唯一实例,除了不使用字符串之外,它对于您想要完成的任务来说有点矫枉过正(在我看来)。
希望有帮助!
If you wanted to ensure uniqueness of strings in the array you could do a couple of things... first would be to simply use array_unique(). That, or you could create an associative array with the strings as keys as well as the values:
If you wanted to be object-oriented about this, you could do something like:
Then, in your code you can do stuff like:
And iterate the same way:
SplObjectStorage is for tracking unique instances of Objects, and outside of not working with strings, it's a bit overkill for what you're trying to accomplish (in my opinion).
Hope that helps!
或者也许只是使用 __toString() 方法将您的字符串实例化为对象 - 这样您就可以同时拥有它们 - 对象并能够将其用作字符串(var_dump,echo)。
Or Maybe just instantiate your string as an object with __toString() method - that way you can have them both - object and ability to use it as string (var_dump, echo)..