i am having a little problems when storing objects in session. According what i think i understood, if the class is serializable and you include it before calling session_start()
, php automatically serializes/unserializes the object.
In the next example (using qcodo 0.4.22 framework) i can not recover the value of the object:
require(dirname(__FILE__) . '/../includes/prepend.inc.php');
QApplication::QcodoInfo();
if (!isset($_SESSION["person"])) {
$person = Person::LoadById(1);
$_SESSION["person"]=$person;
}
else {
echo "Hello ".$_SESSION["person"]->FirstName;
}
So, in order to work i am forced to do:
require(dirname(__FILE__) . '/../includes/prepend.inc.php');
QApplication::QcodoInfo();
if (!isset($_SESSION["person"])) {
$person = Person::LoadById(1);
$_SESSION["person"]=serialize($person);
}
else {
echo "Hello ".unserialize($_SESSION["person"])->FirstName;
}
开发者_开发问答
With no-qcodo classes i dont need to use serialization. Is it possible to avoid the serialization?
Thanks for providing the link to the forums. You put a very important information in there:
When the object is in the session i get the error: “The script tried to execute a method or access a property of an incomplete object.”
That means, at the time of unserializing the session, the class definitions are not already loaded. The unserialization is done at session_start
. So first load all class definitions and then start the session. Helpful in that context are:
- Autoloading Classes Docs
- Define
unserialize_callback_func
(a PHP setting, seeunserialize
Docs). Everytime an undefined class should be instantiated, it'll be called, so you can load the classes via a callback, like with Autloading (Autoloading comes first, you can use this one for debugging however). - Related: (The title of the question is misleading, the interesting part is the answer:) Why is unserialize_callback_func needed when spl_autoload_register is already used?
So try to find out in which like the session gets started. Maybe you only need to start it later, the PHP manual has more information about sessions in general and how to configure when it starts, there is an option to auto-start sessions, maybe you're running into that, but that's just an assumption, there is more to configure and I'm not fluent how Qcodo loads classes.
- PHP Session Runtime Configuration Docs
- Session Introduction Docs
Thanks!. I am closer to the solution with your comment.
Looking into qcodo core i see the place where session is initialized..... and it is done before including the classes.
The problem is that the class is included in the moment i make a call on it (how can is that possible?). I have written an echo sentence in the class file, and that echo appears in the moment where i use the class. For this, i dont know when to call session_start() in order to fix the problem.
EDIT: i have discovered the "autoload" feature in php. Is for that no?. So my question is if it is possible to keep the autoload and start the session in the right moment
EDIT2: i found finally the solution. In qcodo forums suggested me several options. Finally i had to include the data classes before qcodo calls the session_start function. Thx to everyone
精彩评论