phpsessionobjectcodeigniter-2

PHP - The script tried to execute a method or access a property of an incomplete object


I'm using CI and I have a UserModel that selects the user based on login information and sets a userVO and add this userVO in a session like this:

$this->session->set_userdata('user', $userVO);

When I try to access this session it return me this error:

Message: main() [function.main]: The script tried to execute a method 
or access a property of an incomplete object. Please ensure that the 
class definition "UserVO" of the object you are trying to operate on 
was loaded _before_ unserialize() gets called or provide a __autoload() 
function to load the class definition.

I have found a "solution", I need CI to load the UserVO class before session class and it works.

The problem is that I have lots os VO classes and I'll need them inside the session and is a bad thing to autoload them because I won't need them all at the same time.

Is there any workaround?

Thanks in advance for any help.


Solution

  • Whats going on is that you are saving an instance of the class to the session. In order to restore it, you first need to load the base class it is an instance of. You likely have lots of "instances" of the VO class, rather than lots of VO classes. You just need to load the file that has the class declaration.

    The class instance really only contains what has changed from the base class, not the whole class. So it needs the underlying class to know what the "defaults" are.