为什么我不能将PHP类实例存储为SESSION变量 [英] Why can't I store a PHP class instance as a SESSION variable

查看:110
本文介绍了为什么我不能将PHP类实例存储为SESSION变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,通过Dojo Ajax xhrGet调用以两种方式调用。
第一次使用init参数调用时,会导致脚本创建StateList类的实例并读入状态名称文件。

  session_start(); 
@include('StateList.php');
require_once('phplog.php');

//开始执行
$ comd = $ _ GET ['nexturl'];
if($ comd ==init){
$ st = new StateList(../ data / statestxt.txt);
$ _SESSION ['statefile'] = $ st;第二次和另外的时间,另一个xhrGet调用传递一个getstate参数,第二个参数是getstate。下面的代码试图从SESSION数组中获取StateList类的实例。

  if($ comd ==getstate ){
$ st = $ _SESSION ['statefile'];
phplog(size =。$ st-> getSize());

}



)方法永远不会执行,也不能在重构的StateList类实例上调用任何其他方法



请注意,这是一个PHP脚本,类定义在顶部
,因此类方法应该是已知的和avaialble。



我在这里缺少什么?

解决方案

在调用 session_start()之前的类定义,否则对象不会被正确反序列化,并且将是 __ PHP_Incomplete_Class 。否则你应该工作得很好。


I have a PHP script that is called in 2 ways from a Dojo Ajax xhrGet call. The first time it is called with an "init" argument which causes the script to create an instance of the StateList class and read in a file of state names.

session_start();
@include('StateList.php');
require_once('phplog.php');

//start executing here
$comd=$_GET['nexturl'];
if($comd=="init") {
$st = new StateList("../data/statestxt.txt");
$_SESSION['statefile'] = $st;
}

The second and further times, another xhrGet call passes a "getstate" argument and the following code tries to get the instance ofr the StateList class from the SESSION array.

if($comd =="getstate") {
$st= $_SESSION['statefile'];
phplog("size=".$st->getSize());

}

However, the getSize() method is never executed, nor can I call any other method on the reconstituted StateList class instance.

Note that this is one PHP script that DOES include the class definition at the top and thus the class methods should be known and avaialble.

What am I missing here?

解决方案

You need to include the class definition before you call session_start(), otherwise the object will not be deserialized correctly and will be an instance of __PHP_Incomplete_Class. Otherwise what you have should work fine.

这篇关于为什么我不能将PHP类实例存储为SESSION变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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