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

16,439

Solution 1

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.

Solution 2

You may need to serialize the $st object/variable before you store it. This will ensure that everything is saved to the session. This is definitely the way to go for object oriented code. When you want to use the data again, you must unserialize it.

Solution 3

This is one of those things that's hard to debug in isolation. Storing instantiated objects in PHP Sessions is always a little tricky, and not 100% guaranteed to work. Here's some general debugging tips that may help you figure this out.

First, check your apache error log. Are you getting a "method called on non-object error"? If so, this means you're not getting an object back out of the session. If not, is there an error that indicated your method call is failing for another reason?

Second, check to see what you're really getting out of your session.

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

Third, look at the serialized string value that is being stored in the session itself. Are you actually storing a serialized object? In your dev environment, set the session.save_path ini value in php.ini to something like /tmp, (or use the ini_set method to do the same thing):

session.save_path = "/tmp"

and then examine the files created in /tmp (or whatever folder). You should see a string that starts with:

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

The name of the class that instantiated the object will also be included in there, as well as values saved to properties.

Share:
16,439

Related videos on Youtube

Admin
Author by

Admin

Updated on April 17, 2022

Comments

  • Admin
    Admin about 2 years

    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?

  • Admin
    Admin about 15 years
    If I move the definition as you suggest, the value returned from the session array is null.
  • Tom Haigh
    Tom Haigh about 15 years
    do you have display_errors on and error_reporting E_ALL ? Can you make sure that the first bit is actually running ($comd=="init")?
  • outis
    outis over 12 years
    Session variables are automatically serialized and unserialized.