脚本宝典收集整理的这篇文章主要介绍了php – 对象的真实和稳定的哈希,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试使用“spl_object_hash()”(http://php.net/manual/en/function.spl-object-hash.php),但每次重新加载页面时此函数都会返回另一个值.
对于测试,此对象是稳定的并且保持不变并且完全不变.从来没有“spl_object_hash()”一次又一次地返回不同的结果我重新加载.
@H_404_16@$foo = new stdClass(); PRint_r(spl_object_hash($foo));
– >每次点击刷新时都会有另一个哈希值
我需要一种方法来根据完整的对象创建一个哈希.只要对象没有,该哈希就不应该改变.
我不想为此实现自己的方法(只要没有其他可能解决我的问题),因为我需要一种快速,简单和统一的方法来识别我的对象.
此外,我不想为此目的使用Sessions.
那么我该怎么做才能解决这个问题呢?
@H_404_16@$hash = md5(serialize($myObject));
此外,在存储一些额外数据的对象(如PDO处理程序,文件句柄)中,您可以覆盖__sleep()和__wakeup()方法以获取/设置对象数据,例如来自http://www.php.net/manual/en/language.oop5.magic.php#object.sleep的代码
@H_404_16@<?PHP class Connection { protected $link; private $dsn,$username,$password; public function __construct($dsn,$password) { $this->dsn = $dsn; $this->username = $username; $this->password = $password; $this->connect(); } private function connect() { $this->link = new PDO($this->dsn,$this->username,$this->password); } public function __sleep() { return array('dsn','username','password'); } public function __wakeup() { $this->connect(); } }?>
这应该为您提供良好的对象哈希,更好的是,您可以配置要用于创建哈希的字段.
以上是脚本宝典为你收集整理的php – 对象的真实和稳定的哈希全部内容,希望文章能够帮你解决php – 对象的真实和稳定的哈希所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。