php: Storing objects in a session

    On a resource recently recently promoted here, I came across a question about storing objects created in a script in a php session.

    How bad the storage of objects in a session is, in principle, I will not discuss, but simply show how to work with such objects. However, if experts have already found a recipe on php.net , and consider that everything is trivial, I completely agree with them. However, if you are a little unaware ...

    So, we set ourselves the goal of putting an object, say, a certain user into the session.

    Approximately our files will look like this:

    myclass.php:


    index.php: Now let's figure out what we are doing. The first time you run the index.php script, we create an object of class myclass and put it in a session. In any subsequent case (until we destroy the session or destroy the “link” to it - in the form of cookies in the browser, for example) we load the session in which our object already exists. However, instead of being an instance of the MyClass class, var_dump () pleases us with the __PHP_Incomplete_Class class:
    user = 'me';
        $_SESSION['classTst'] = $myclass;
        echo "Class is into session array now";
    }
    else {
        echo "\$_SESSION['classTst'] dump: \n\n";
        var_dump($_SESSION['classTst']);
    }
    







    $ _SESSION ['classTst'] dump: 
    object (__ PHP_Incomplete_Class) # 1 (2) {
      ["__PHP_Incomplete_Class_Name"] =>
      string (7) "MyClass"
      ["user"] =>
      string (2) "me"
    }
    


    Everything is very simple. When initializing a session, php stumbles upon the fact that it has an instance of the MyClass class in $ _SESSION ['classTst'] (those who wish can look at the contents of the file (if you have sessions in files) of the session and make sure that our serialized object will lie there, with a description of what class it is an instance of). But (surprise!), As can be seen from the script, php at the start of the session knows nothing about the existence of the MyClass class. Therefore, a completely logical interpreter further perceives this object precisely as an object of a certain __PHP_Incomplete_Class.

    But if we change the order of the start of the session and class loading: , like, voila, we get what we want, and:
    user = 'me';
        $_SESSION['classTst'] = $myclass;
        echo "Class is into session array now";
    }
    else {
        echo "\$_SESSION['classTst'] dump: \n\n";
        var_dump($_SESSION['classTst']);
    }
    





    $ _SESSION ['classTst'] dump: 
    object (MyClass) # 1 (1) {
      ["user"] =>
      string (2) "me"
    }
    


    In general, everything is really simple: all the objects that you want to store in the session must be known to the interpreter BEFORE you start the session.

    Thus, for example, if your standard is session.auto_start On, storing objects in a session is not your destiny. Also, if you rely on frameworks whose authors decided for you where and when which objects to load (and you rely entirely on the authors), storing certain objects in a session can become just a deadly number (in the case of an object with other nested objects , called “on demand” somewhere before the execution, say, of the controller, when the session started long ago). In the case of a compound object (containing 2-3 service objects in properties, for example), a reasonable question may arise: what is “cheaper” - throw everything in a session, or retrieve an object with one or two queries from the database (or database cache)?

    So what to store or not to store objects in a session, and how to do it based on the realities of your project - you decide anyway :)

    Also popular now: