本文介绍了我可以在 $_SESSION 空间中存储类实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Class User{
public $id;
public $username;
public $password;
public $email;
public $steam;
public $donator;
public $active;
public function __construct($username, $email, $password, $id, $active, $donator, $steam){
$this->id = $id;
$this->username = $username;
$this->password = $password;
$this->email = $email;
$this->steam = $steam;
$this->donator = $donator;
$this->active = $active;
}}
是我的班级(简体)
以下是我的代码:
$_SESSION['loggedIn'] = $user;
$user 是 User 的类实例
$user is a class instance of User
现在这就是 print_r($_SESSION['loggedIn']) 显示给我的:
now this is what print_r($_SESSION['loggedIn']) shows me:
__PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => User
[id] => 22
[username] => xxxx
[password] => xxxx
[email] => xxxx
[steam] => 1234567
[donator] => 0
[active] => 1
)
其中 xxxx 是正确的值.
in which xxxx are values that are correct.
但是当我尝试从会话中检索数据时.像这样:$_SESSION['loggedIn']->username"它向我返回一个空值.
but when i try to retrieve data from my session. like so: "$_SESSION['loggedIn']->username" it returns a null value to me.
推荐答案
您必须先将对象序列化为字符串:
You must first serialize the object in to a string:
$_SESSION['user'] = serialize($user);
和:
$user = unserialize($_SESSION['user']);
只需确保在反序列化对象之前首先定义了类.
Just make sure that the class is first defined before unserializing the object.
这篇关于我可以在 $_SESSION 空间中存储类实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!