Hi, all

Need some idea about implement a object, and I need keep the object within the session, that means I have to pass this object between pages. How can I do it?

I can think of two ways

by register the object in session variable. or serialize the object and pass by url or POST. Which way is better? Or should I do another way?

Thanks

    use sessions. remember that parent class definitions have to come before refering to object instances.

      Thanks! devinemke. Also just wonder how much of your code using OOP

        Also, how can I pass the object by session? I tried

        $_SESSION['obj'] = new MyClass();

        but when I retrieve the $_SESSION['obj'], it's not an object anymore. How can I deal with it?

          re-read my earlier post. your class definition has to come before you start your session.

          note: this will not work if you have session.auto_start ON in your php.ini

            you are right, I have to ture the session_autostart to off to make it work.

            There's another issue. I was passing a datasource into the class, and after pass by session, the datasource object gets error:

            Fatal error: Unknown(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition <b>db_mysql</b> of the object you are trying to operate on was loaded before the session was started

            How to with the opened datasource in object then?

              heres how i do it, dont know if this will help your issue though

              <?php
              
              require_once 'object.class.php';
              
              session_start();
              
              
              $object =& $_SESSION['object'];
              if (!is_object($object)) {
                  $object = new object();
              }
              
              ?>
              

              edit- i just noticed, you said open datasource. to my knowledge you cant serialize resources.

                Thanks. I solved it by define datasource outside of the class.

                  Write a Reply...