如何在PHP的会话中存储对象? [英] How can I store objects in a session in PHP?

查看:92
本文介绍了如何在PHP的会话中存储对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想使用SessionHandler或$ _SESSION之类的类在会话中存储或保存对象,我已经看到序列化对象是可能的,并且我不想丢失方法该对象实例的.. 我看到有可能将其序列化,但是我想存储的对象是由PDOStatement :: fetchObject()创建的,尽管实例类是"Users",但出现此错误:

Hello I would like to store or save an object inside a session,using classes like SessionHandler or arrays $_SESSION,I've seen that it is possible if I serialize the object,and I don't want to lose the methods of that object instance.. I've seen seralizing it is possible but the objects what I want to store is created by PDOStatement::fetchObject() althought the instance class is "Users" I get this error:

PDOException:您无法序列化或反序列化PDO实例 为什么?它不是PDO实例.

PDOException: You cannot serialize or unserialize PDO instances Why? It is not a PDO instance..

对不起,我是西班牙人,我的英语说得不太好. 谢谢

Sorry I am spanish and I don't speak english very well.. Thanks

推荐答案

PHP的本机$_SESSION会话透明地序列化和反序列化支持PHP

PHP's native $_SESSION sessions transparently serialize and unserialize objects that support PHP's serialization protocol or the Serializable interface. You do not need to explicitly serialize them.

PHP无法序列化 resources ,因为这些是PHP无法控制的一些有状态资源.这就是为什么您不能序列化PDOPDOStatement对象的原因.

PHP cannot serialize resources because these are handles to some stateful resource outside PHP's control. This is why you cannot serialize PDO or PDOStatement objects.

默认情况下,通过保存所有属性名称和值来序列化对象,并通过创建具有相同类的对象(不调用构造函数)并直接设置序列化的属性来取消序列化.您可以使用__sleep__wakeup魔术方法或通过实现Serializable接口自定义对象的序列化行为.但不是两者!如果使用implements Serializable,则会忽略__sleep__wakeup.

By default an object is serialized by saving all property names and values and unserialized by creating an object with the same class (without invoking the constructor) and directly setting the serialized properties. You can customize serialization behavior for your objects using the __sleep and __wakeup magic methods or by implementing the Serializable interface. But not both! If you use implements Serializable, __sleep and __wakeup are ignored.

一个重要说明:使用对象序列化时,您必须在反序列化之前加载类定义(或具有可以加载它的自动加载器),并且 >必须匹配被序列化的对象的类定义.类定义未存储在序列化数据中.

One important note: when using object serialization, you must have the class definition loaded before you unserialize (or have an autoloader that can load it) and it must match the class definition of the object that was serialized. Class definitions are not stored in the serialized data.

例如,假设您具有以下条件:

For example suppose you have the following:

class Test {
    public $version = 1;
    protected $abc;
    public function setAbc($abc) {
        $this->abc = $abc;
    }
}

$t = new Test();
$t->setAbc(123);
$_SESSION['mytest'] = $t;

现在想象一下,您有一天将Test更改为这样:

Now imagine you change Test one day to be like this instead:

class Test {
    public $version = 2;
    private $def;
    public function setDef ($def) {
        $this->def = $def;
    }
}

现在假设您将在Test为版本1时序列化的对象加载到新代码中:

Now suppose you load into your new code an object serialized when Test was at version 1:

$t = $_SESSION['mytest']; // this was stored yesterday, when Test was version 1

var_dump($t)

您将获得:

object(Test)#1 (3) {
  ["version"]=>
  int(1)
  ["def":"Test":private]=>
  NULL
  ["abc":protected]=>
  int(123)
}

此外,您不能使用旧方法:

Furthermore, you can't use old methods:

if ($t->version == 1) { // Check for class version
    $t->setAbc(345); // "Fatal error: Call to undefined method Test::setAbc()"
}

这篇关于如何在PHP的会话中存储对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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