php PDOException: 您不能序列化或反序列化 PDO 实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8700702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDOException: You cannot serialize or unserialize PDO instances
提问by MaxiNet
I tried to cache my User object in PHP with memcache, but I get an error while using PDO. I added a __sleep and a __wakeup function.
我尝试使用 memcache 在 PHP 中缓存我的 User 对象,但是在使用 PDO 时出现错误。我添加了一个 __sleep 和一个 __wakeup 函数。
User.php
用户名
/**
* @var PDO
*/
protected $db;
public function __construct()
{
$this->db = getInstanceOf('db');
}
public function __destruct()
{
}
public function __sleep()
{
return array('db');
}
public function __wakeup()
{
$this->db = getInstanceOf('db');
}
getInstanceOf('db') returns a pdo object...
getInstanceOf('db') 返回一个 pdo 对象...
Returns the following error:
返回以下错误:
PDOException: You cannot serialize or unserialize PDO instances in /var/www/test/User.php on line 41
PDOException:您不能在第 41 行的 /var/www/test/User.php 中序列化或反序列化 PDO 实例
回答by hakre
It is likely that $this->db
is a PDO object. PDO objects can not be serialized.
这很可能$this->db
是一个 PDO 对象。PDO 对象不能被序列化。
Remove that object on __sleep()
and add it back at __wakeup()
(which is what you already do in the later case):
删除该对象__sleep()
并将其添加回__wakeup()
(这是您在后一种情况下已经执行的操作):
public function __sleep()
{
return array();
}
You can not serialize objects that can not be serialized. But you tried, so you got the exception. That's basically the whole issue. Just don't tell PHP to serialize objects that can't be serialized.
您无法序列化无法序列化的对象。但是你试过了,所以你得到了例外。这基本上就是整个问题。只是不要告诉 PHP 序列化无法序列化的对象。