PHP 序列化为哈希函数的副作用
我正在寻找在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经提到您正在尝试使用对象作为哈希中的键来存储附加数据。
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.
对象作为键: SplObjectStorage
Objects as keys: SplObjectStorage
您可以使用
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.