PHP PDO - 无法序列化或反序列化 PDO 实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10936636/
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
PHP PDO - cannot serialize or unserialize PDO instances
提问by laukok
I need to pass a PDO connection into a cartclass from a controller,
我需要将 PDO 连接cart从controller,
function __construct($connection)
{
$this->cart = new cart($connection);
}
but I think the problem is with serialize()
但我认为问题在于 serialize()
public function render_page()
{
if (!isset($_SESSION[SESSION_CART]))
{
$cart = $this->cart;
}
else
{
$cart = unserialize($_SESSION[SESSION_CART]);
}
$_SESSION[SESSION_CART] = serialize($cart);
}
I get this error,
我收到这个错误,
Fatal error: Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances' in C:\wamp\www\store_2012_MVC\local\controllers\class_base_extended_cart.php:89 Stack trace: #0 [internal function]: PDO->__sleep() #1 C:\wamp\www\store_2012_MVC\local\controllers\class_base_extended_cart.php(89): serialize(Object(cart)) #2 C:\wamp\www\store_2012_MVC\local\controllers\class_factory.php(75): base_extended_cart->render_page() #3 C:\wamp\www\store_2012_MVC\index.php(69): factory->render() #4 {main} thrown in C:\wamp\www\store_2012_MVC\local\controllers\class_base_extended_cart.php on line 89
致命错误:C:\wamp\www\store_2012_MVC\local\controllers\class_base_extended_cart.php:89 中的消息“您无法序列化或反序列化 PDO 实例”的未捕获异常“PDOException”:89 堆栈跟踪:#0 [内部函数]:PDO- >__sleep() #1 C:\wamp\www\store_2012_MVC\local\controllers\class_base_extended_cart.php(89): serialize(Object(cart)) #2 C:\wamp\www\store_2012_MVC\local\controllers\class_factory. php(75): base_extended_cart->render_page() #3 C:\wamp\www\store_2012_MVC\index.php(69): factory->render() #4 {main} 扔在 C:\wamp\www\store_2012_MVC \local\controllers\class_base_extended_cart.php 第 89 行
How can I fix this?
我怎样才能解决这个问题?
Or can I use something else instead of serialize()?
或者我可以用别的东西代替serialize()吗?
EDIT:
编辑:
I tried it with __sleepand __wakeupmagic methods but still get the same error,
我试了一下 __sleep,并__wakeup神奇的方法,但仍然得到了同样的错误,
class database_pdo
{
# database handler
protected $connection = null;
# make a connection
public function __construct($dsn,$username,$password)
{
try
{
$this->connection = new PDO($dsn, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
# call the get_error function
$this->get_error($e);
}
}
# don't forget to add getter method to get $this->connection, it's just a good practice.
public function get_connection()
{
return $this->connection;
}
public function __sleep()
{
return array('connection');
}
public function __wakeup()
{
$this->connection;
}
}
采纳答案by Chris Gutierrez
Take a look at the __sleep and __wakeup magic methods. http://us.php.net/manual/en/language.oop5.magic.php#object.sleep
看看 __sleep 和 __wakeup 魔法方法。 http://us.php.net/manual/en/language.oop5.magic.php#object.sleep
They allow you to specify which properties get serialized and which get ignored. The issue there is that you'll need to regularly pass in an instance of your PDO object.
它们允许您指定哪些属性被序列化,哪些被忽略。问题在于您需要定期传入 PDO 对象的实例。
回答by Tiberiu-Ionu? Stan
PDO objects contain active links to databases (which may have a transaction initiated or db session settings and variabiles).
PDO 对象包含到数据库的活动链接(可能具有已启动的事务或数据库会话设置和变量)。
You cannot serialize a PDO object because the above would get lost and cannot be re-established automatically.
您无法序列化 PDO 对象,因为上述内容会丢失并且无法自动重新建立。
You should redesign your classes to access the current database link statically using a separate class (dedicated for holding db connections), instead of saving a reference in a member variable (I supose this is happening when you do new cart($connection))).
您应该重新设计您的类以使用单独的类(专用于保存数据库连接)静态访问当前数据库链接,而不是在成员变量中保存引用(我假设这在您执行 new cart($connection)) 时发生) .

