无法在 PHP 中序列化基于单例的对象
我有一个基于单例设计的对象,用于用户身份验证。因为该对象是每个用户的对象,所以我希望在执行结束时将该对象自动存储在会话变量中。但是,每当我尝试在内部或外部序列化对象时,我都会得到一个空字符串。
以下是基本类,减去不相关的函数:
<?php
/**
* The user class is intended to handle information about authenticated users. Information contained in this class is to be stored
* in SESSION['session_user'] as a serialized object.
*/
class User {
// Reference to the single User instance
private static $_instance;
// User levels
const GUEST = 0;
const USER = 1;
const ADMINISTRATOR = 3;
// Information about the account
private $_username;
private $_userid;
private $_userlevel;
// Information about the user, for preventing session hijacking
private $_ipaddress;
private $_useragent;
private function __construct() {
// Set the visitor's information
// Set the default information
}
public static function getUser() {
// Check if a user object has been created
if (!isset(self::$_instance)) {
// Check if the object is stored in the user session
if (isset($_SESSION['session_user'])) {
self::$_instance = unserialize($_SESSION['session_user']);
//unset($_SESSION['session_user']);
echo 'Unserializing user';
} else {
$c = __CLASS__;
self::$_instance = new $c;
echo 'Creating new user';
}
}
return self::$_instance;
}
function __wakeup() {
// First, check that the user agent has not changed
// Check that the IP has not changed
}
function __destroy() {
$_SESSION['session_user'] = serialize(self::$_instance);
echo serialize(self::$_instance);
print_r($_SESSION);
}
public function __set($index, $value) {
return NULL;
}
public function __get($index) {
// Determine which value to return
}
public function authenticate($inUsername, $inPassword) {
// Authenticate the user
}
}
?>
任何时候我在对象上调用序列化,无论是在内部使用serialize($this)或serialize(self::$_instance)在__destroy方法中,还是在外部使用serialize($user) ,我得到一个空字符串。但是,我知道该对象存在,因为我可以从中获取有关经过身份验证的用户的数据。
I have an object, based on the Singleton design, that I use for user authentication. Because the object is a per-user object, I want the object to be stored automatically in a session variable at the end of execution. However, any time I try to serialize the object, either internally or externally, I get an empty string.
The following is the basic class, minus irrelevant functions:
<?php
/**
* The user class is intended to handle information about authenticated users. Information contained in this class is to be stored
* in SESSION['session_user'] as a serialized object.
*/
class User {
// Reference to the single User instance
private static $_instance;
// User levels
const GUEST = 0;
const USER = 1;
const ADMINISTRATOR = 3;
// Information about the account
private $_username;
private $_userid;
private $_userlevel;
// Information about the user, for preventing session hijacking
private $_ipaddress;
private $_useragent;
private function __construct() {
// Set the visitor's information
// Set the default information
}
public static function getUser() {
// Check if a user object has been created
if (!isset(self::$_instance)) {
// Check if the object is stored in the user session
if (isset($_SESSION['session_user'])) {
self::$_instance = unserialize($_SESSION['session_user']);
//unset($_SESSION['session_user']);
echo 'Unserializing user';
} else {
$c = __CLASS__;
self::$_instance = new $c;
echo 'Creating new user';
}
}
return self::$_instance;
}
function __wakeup() {
// First, check that the user agent has not changed
// Check that the IP has not changed
}
function __destroy() {
$_SESSION['session_user'] = serialize(self::$_instance);
echo serialize(self::$_instance);
print_r($_SESSION);
}
public function __set($index, $value) {
return NULL;
}
public function __get($index) {
// Determine which value to return
}
public function authenticate($inUsername, $inPassword) {
// Authenticate the user
}
}
?>
Any time I call serialize on the object, either internally in the __destroy method using serialize($this) or serialize(self::$_instance), or externally using serialize($user), I get an empty string. However, I know the object exists since I can get data out of it about an authenticated user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个神奇的函数被称为
__destruct
,而不是__destroy
。完毕 ;)The magic function is called
__destruct
, not__destroy
. Done ;)