0

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
yauros
  • 157
  • 1
  • 3
  • 8
  • Which class is `Person`? – hakre Sep 14 '11 at 13:11
  • It's a "Table class" taken from an example database in Qcodo site. I only want to illustrate the impossibility of storing objects in session without serializing – yauros Sep 14 '11 at 18:25
  • Does that table class has a specific and concrete name? Probably something that can be looked up on http://api.qcodo.com/ ? I just would like to understand the specifics. That you have a problem to deal with a concrete instance in storing it into the session is already clear to me. – hakre Sep 14 '11 at 21:28
  • The name is Person both for the table and the class. The problem is the same for all the qcodo table classes. This is the link for the database script: http://examples.qcodo.com/examples/view_source.php/0/1/sql_server.sql.The strange thing is that for a previous version of qcodo i hadn't this problem – yauros Sep 15 '11 at 05:57
  • By default the session serializes all values - as you wrote. That's by PHP itself. As long as Qcodo does not override this behavior, this does work. I was looking into Qcodo code to locate the classes of *Person*, but had problems to find them all, especially *Person* itself. For the classes I was able to locate (*QCodeGenBase *; *QCodeGen*), I could not find anything related to serialize either. But that must not mean much, because I had problems to follow the include path. You should file a bug report with the project IMHO to find out more. – hakre Sep 15 '11 at 10:12
  • Thanks for your answers. I opened a thread in the qcodo forum and we are discussing about that. It is very strange... – yauros Sep 15 '11 at 10:59
  • Sure!:http://www.qcodo.com/forums/forum.php/6/4397/lastpage – yauros Sep 15 '11 at 11:03

2 Answers2

1

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:

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.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
0

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

yauros
  • 157
  • 1
  • 3
  • 8
  • If you can, please add some code how you did it with your answer and accept the answer so this question gets marked as solved. Thanks! And glad you could solve it! – hakre Sep 16 '11 at 19:07