在会话变量中存储 PHP 对象 [英] Storing A PHP Object In A Session Variable

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

问题描述

我是 OOP 的新手,正在编写我的第一堂课.我为一家保险经纪人工作,正在尝试使用类来存储有关报价的内容,并将对象存储为会话变量.

事情是,当我查看会话变量时,我得到:

sessionName__PHP_Incomplete_Class 对象([__PHP_Incomplete_Class_Name] =>我的课[brokerId] =>

谁能告诉我为什么它显示不完整的类名?

解决方案

确保在调用 session_start() 之前存在类定义,例如

require_once 'class.MyClass.php';session_start();

或设置一个 unserialize_callback_func 来尝试加载类定义,如 http://docs 所述.php.net/function.unserialize.

这也可以通过 spl_autoload_register() 来完成.例如

spl_autoload_register(function($name) {//只是一个演示...这可能是不安全的 ;-)require_once 'class.'.$name.'.php';});session_start();echo '

';var_dump($_SESSION);echo '</pre>';

I'm new to OOP and am writing one of my first classes. I work for an insurance broker and am trying to use a Class to store things about a quote, and store the Object as a session variable.

Thing is, when I view the session variables, I get:

sessionName         

__PHP_Incomplete_Class Object
(
    [__PHP_Incomplete_Class_Name] => myClass
    [brokerId] => 

Can anyone tell me why it's showing incomplete class name?

解决方案

Make sure that either the class definition is present before session_start() is called, e.g.

require_once 'class.MyClass.php';
session_start();

or set an unserialize_callback_func that will try to load the class definition as described at http://docs.php.net/function.unserialize.

edit: this can also be done via spl_autoload_register(). E.g.

spl_autoload_register(function($name) {
    // only a demo ...this might be insecure ;-)
  require_once 'class.'.$name.'.php';
});
session_start();
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';

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

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