Why can't I store an instance of a PHP class as a SESSION variable

I have a PHP script that is called in two ways from a Dojo Ajax xhrGet call. The first time it is called with the argument "init", which causes the script to instantiate the StateList class and read in the state name file.

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;
}

A second or later time, another call to xhrGet passes the argument "getstate", and the following code tries to get an instance of the StateList class from the SESSION array.

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

}

However, the getSize () method is never executed, and I cannot call any other method on the restored instance of the StateList class.

Please note that this is one PHP script that includes the class definition at the top and therefore class methods must be known and accessible.

What am I missing here?

+5
6

session_start(), __PHP_Incomplete_Class. .

+14

serialize $st object/variable . , . , , - . , unserialize it.

+1

, . PHP- , 100% . , .

apache. , - ? , , . , , , ?

-, , .

if($comd =="getstate") {
    $st= $_SESSION['statefile'];
    //get the class of st
    phplog("instance=".get_class($st));

    //get a reflection dump of st
    $ref = new ReflectionClass($st);
    $string = $ref->__toString();       
    phplog("reflection=".$string);
}   

-, , . ? dev session.save_path ini php.ini - /tmp ( ini_set ):

session.save_path = "/tmp"

, /tmp ( ). , :

statefile:O:..........

, , , .

+1

, . PHP, . . , " " , .

0

session.auto_start ? , , -:

session.auto_start, - auto_prepend_file, , .

http://php.net/manual/en/intro.session.php

, / PHP, session.auto_start .

0

:

    include('StateList.php');
    require_once('phplog.php');
    // start your session after including your class file
    session_start();

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

    if($comd =="getstate") {
    // the ampersand creates a reference, preserving any further changes to your session data
    $st = &$_SESSION['statefile'];
    phplog("size=".$st->getSize());
    }
0

All Articles