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?
The
SplObjectStorageis what its name says: a storage class for storing objects. In contrast to some other programming languagesstringsare 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
SplObjectStoragefor PHP < 5.3.0 and to get a better understanding of what it does.SplObjectStoragestores 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.