开发者

Objects to strings, unique keys in PHP

I was reading around about the Observer pattern, and found a dated article. Having read through, I noticed an interesting mention in this paragraph:

开发者_StackOverflow社区The key methods to look at here are attach(), detach(), and notify(). attach() and detach() handle adding and removing observers. We use a little trick here. Objects quoted in string context resolve to a unique identifier (even if __toString() is defined). You can use this fact to build keys for an associative array. The notify() method cycles through all attached observers, calling update() on each. The UploadManager class calls notify() whenever it has something important to report on upload and on error, in this case.

Which references this example:

function attach(UploadObserver $obs) {
    $this->observers["$obs"] = $obs;
}

Now as mentioned, this article is dated. Casting objects to strings of course no longer works in this manner (I run 5.3.6 on my dev box, and push it for all client projects) but I'd like to achieve similar functionality. I can only think of (something like) this:

function attach(Observer $observer){
    $this->_observers[md5(serialize($observer))] = $observer;
}

function detach(Observer $observer){
    unset($this->_observers[md5(serialize($observer))]);
}

I'm curious, are there any other efficient ways to achieve this; creating a unique key from the object itself.

Caveat: I don't want to get into defined keys, I use those often enough with other repositories and such, implementing __set($key, $value), etc.

Note: I understand MD5 isn't ideal.


Update: Just found spl_object_hash, and I assume this is likely my best choice, however feel free to share your thoughts.


You're right that does not work that way any longer. You might want to use some other function instead: spl_object_hash()

function attach(Observer $observer){
    $this->_observers[spl_object_hash($observer)] = $observer;
}

function detach(Observer $observer){
    unset($this->_observers[spl_object_hash($observer)]);
}

The serialization based approach has a design problem btw: I stops working when objects are identical by value or in other words if objects return the same serialized value, e.g. NULL. This is fully controllable by the objects themselves when they implement the Serializable interface.


Have you tried the SPL object hash function?


Alternatively you could use SplObjectStorage directly.

Like:

function __construct(...){
    $this->_observers = new SplObjectStorage;
}

function attach(Observer $observer) {
    $this->_observers[$observer] = $observer;
}

function detach(Observer $observer){
    unset($this->_observers[$observer]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜