PHP 序列化为哈希函数的副作用

发布于 2024-10-22 00:47:12 字数 1704 浏览 8 评论 0原文

我正在寻找在 PHP 中创建一种 HashMap 类。为了能够构建 PHP 的关联数组功能,我有一个哈希函数,它应该能够接受任何变量(基元或对象)并将其转换为字符串以用作数组键。

对于这个哈希函数,我正在考虑使用 serialize(),但我注意到 PHP 在调用该函数时会在对象上调用 __sleep()。我假设这可能有问题。我说得对吗?

如果是这样,我可以使用什么来获取原始数据类型或对象的哈希值?我确实查看了 spl_object_hash(),但它的结果似乎不太唯一,因为它使用了参考位置,而这些参考位置似乎被重用了?

有什么想法吗?谢谢

更新:如果有人感兴趣,这就是(粗略地说)我最终得到的结果。 Collection接口可以忽略。当然,欢迎任何改进。 哦,目前还没有删除方法。

<?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));
                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;
    }
}

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));
                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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你而言 2024-10-29 00:47:12

您已经提到您正在尝试使用对象作为哈希中的键来存储附加数据。

PHP-5.3 中的标准 SPL 对象存储 类就是为此用途而设计的案例,尽管使用起来有点时髦。值得庆幸的是,它可以充当数组。

现在,它只能用于存储实际对象,而不能用于存储基元。这可能会给您的用例带来问题,但这可能是您将对象存储为键的最佳选择。

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.

吃素的狼 2024-10-29 00:47:12

对象作为键: SplObjectStorage

Objects as keys: SplObjectStorage

蓝戈者 2024-10-29 00:47:12

您可以使用 md5

http://php.net/manual 进行哈希处理/en/function.md5.php

但是,当然您需要为所涉及的对象提供可靠且唯一的 toString

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文