开发者

Side effects of PHP's serialize as hash function

开发者 https://www.devze.com 2023-02-16 08:29 出处:网络
I\'m looking to create a sort of HashMap class in PHP. For the sake of being able to build upon PHP\'s associative array functionality, I have a hash function, which should be able to take any variabl

I'm looking to create a sort of HashMap class in PHP. For the sake of being able to build upon PHP's associative array functionality, I have a hash function, which should be able to take any variable (primitive or object) and turn it into a string for use as an array key.

For this hash function, I am thinking of using serialize(), but I noticed that PHP calls __sleep() on an object when that function is called. I'm assuming this could be problematic. Am I right?

If so, what can I use to get a hash of either a primitive data type or of an object? I did look at spl_object_hash(), but its results seem less than unique, as it uses reference locations, which appear to be reused?

Any thoughts? Thanks

Update: If anyone's interested, this is (roughly speaking) what I ended up with. The Collection interface can be ignored. Any improvements welcome, of course. Oh, and there isn't a remove method yet.

<?php

include_once 'Collection.php';

class HashMap implements Collection {

    private $data;
    private $hashes;

    public static function createEmpty() {
        return new HashMap();
    }
    public function __construct() {

        $this->data = new \SplObjectStorage();
        $this->hashes = array();
    }

    public function add($key, $value) {
        // var_dump($this->hash($key));
        $this->data->offsetSet($this->hash($key), $value);
    }

    private function hash($key) {
        if (!is_object($key)) {
            if (isset($this->hashes[$key])) {
                return $this->hashes[$key];
            } else {
                $obj = new PrimitiveAsObject(serialize($key));开发者_JAVA百科
                return ($this->hashes[$key] = $obj);
            }
        } else {
            return $key;
        }
    }

    public function get($key) {
        $key = $this->hash($key);

        if ($this->data->contains($key)) {
            return $this->data->offsetGet($key);
        } else {
            return null;
        }
    }

}
class PrimitiveAsObject {
    private $val;
    public function __construct($v) {
        $this->val = $v;
    }
}


You've mentioned that you're trying to use objects as keys in a hash, to store additional data.

The standard-in-PHP-5.3 SPL Object Storage class was designed for this use case, though it's kind of funky to use. It can behave as an array, thankfully.

Now, it can only be used to store actual objects, not primitives. This may pose a problem for your use case, but it's probably the best thing you've got for storage of objects as keys.


Objects as keys: SplObjectStorage


You can hash with md5

http://php.net/manual/en/function.md5.php

But of course you need a reliable and unique toString for objects involved.

0

精彩评论

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