PDOException:您不能序列化或反序列化PDO实例 [英] PDOException: You cannot serialize or unserialize PDO instances

查看:566
本文介绍了PDOException:您不能序列化或反序列化PDO实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用memcache在PHP中缓存我的User对象,但是在使用PDO时出现错误.我添加了__sleep和__wakeup函数.

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.

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')返回一个pdo对象...

getInstanceOf('db') returns a pdo object...

返回以下错误:

PDOException:您不能在第41行的/var/www/test/User.php中序列化或反序列化PDO实例

PDOException: You cannot serialize or unserialize PDO instances in /var/www/test/User.php on line 41

推荐答案

$this->db很可能是PDO对象. PDO对象无法序列化.

It is likely that $this->db is a PDO object. PDO objects can not be serialized.

__sleep()上删除该对象,然后将其重新添加到__wakeup()(这在后面的情况中已经做过):

Remove that object on __sleep() and add it back at __wakeup() (which is what you already do in the later case):

public function __sleep()
{
    return array();
}

您无法序列化无法序列化的对象.但是您尝试了,所以得到了例外.基本上,这就是整个问题.只是不要告诉PHP序列化无法序列化的对象.

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.

这篇关于PDOException:您不能序列化或反序列化PDO实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆