On the recently published resource here, I stumbled upon the question of storing objects created in the script in a php session.
How bad the practice is, in principle, the storage of objects in a session, I will not discuss, but just show you how to work with such objects. However, if experts have already found a recipe on
php.net , and believe that everything is trivial, I completely agree with them. However, if you do not know a bit ...
So, we set out to put an object in a session, say, a certain user.
About our files will look like this:
')
myclass.php:
<?php class MyClass { public $user; }
index.php:
<?php session_start(); require 'myclass.php'; header("content-type: text/plain"); if (!isset($_SESSION['classTst'])) { $myclass = new MyClass(); $myclass->user = 'me'; $_SESSION['classTst'] = $myclass; echo "Class is into session array now"; } else { echo "\$_SESSION['classTst'] dump: \n\n"; var_dump($_SESSION['classTst']); }
<?php session_start(); require 'myclass.php'; header("content-type: text/plain"); if (!isset($_SESSION['classTst'])) { $myclass = new MyClass(); $myclass->user = 'me'; $_SESSION['classTst'] = $myclass; echo "Class is into session array now"; } else { echo "\$_SESSION['classTst'] dump: \n\n"; var_dump($_SESSION['classTst']); }
And now let's figure out what we are doing.
In the first launch of the index.php script, we create an object of the class myclass and put it into the session. In any subsequent case (until we destroy the session or destroy the “link” to it - in the form of a cookie in the browser, for example) we load the session in which our object already exists. However, instead of being an instance of the class MyClass, var_dump () “pleases” us with the class "__PHP_Incomplete_Class":
$ _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 the session is initialized, php stumbles upon the fact that it has an instance of the class MyClass 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 be there, with a description of which class it is an instance of). But (surprise!), As can be seen from the script, at the time of the session start up, php knows nothing about the existence of the class MyClass. Therefore, a completely logical interpreter further perceives this object as an object of a certain __PHP_Incomplete_Class.
But if we change the order of the session start and class loading:
<?php require 'myclass.php'; session_start(); header("content-type: text/plain"); if (!isset($_SESSION['classTst'])) { $myclass = new MyClass(); $myclass->user = 'me'; $_SESSION['classTst'] = $myclass; echo "Class is into session array now"; } else { echo "\$_SESSION['classTst'] dump: \n\n"; var_dump($_SESSION['classTst']); }
<?php require 'myclass.php'; session_start(); header("content-type: text/plain"); if (!isset($_SESSION['classTst'])) { $myclass = new MyClass(); $myclass->user = 'me'; $_SESSION['classTst'] = $myclass; echo "Class is into session array now"; } else { echo "\$_SESSION['classTst'] dump: \n\n"; var_dump($_SESSION['classTst']); }
,
as, voila, we get what we wanted:
$ _SESSION ['classTst'] dump:
object (MyClass) # 1 (1) {
["user"] =>
string (2) "me"
}
In general, everything is really simple: all objects that you want to store in a session should 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 fate. Also, if you rely on frameworks whose authors have decided for you, where and when what objects to load (and you rely entirely on authors), storing certain specific objects in a session can become just a death number (in the case of an object with other embedded objects , called "on demand" somewhere before the execution, say, of the controller, when the session started a long time 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” to throw everything into a session, or to get an object again with one or two queries from the database (or database cache)?
So, to store or not to store objects in a session, and how to do it based on the realities of your project - it's up to you anyway :)