Jump to content

Storing An Object In A Session


JustinK101

Recommended Posts

So I am storing a custom class object I wrote in a session like:

 

////

// Set session language object

////

$_SESSION['language_object'] = $language;

 

$language is an instance of my Language class.

 

Then I am calling the following block of code from Inside my Language load() method. Basically what I want to do is see if the session already exists storing all Language strings, if so use that.

 

if(isset($_SESSION['language_object'])) {
			$language_obj = $_SESSION['language_object'];
			$this->language_array = $language_obj->get_array();
			return;
		}

 

This is kicking back the following error:

 

Fatal error: Language::load() [<a href='language.load'>language.load</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Language" 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 in Language.php on line 38

 

Any idea what the hell that means?

Link to comment
https://forums.phpfreaks.com/topic/108941-storing-an-object-in-a-session/
Share on other sites

first serialize  the object using serialize(), then save it to session. when u retrieve from session use unserialize().

LIke bellow:

save the object to the session

//saving object to session
$_SESSION['language_object'] = serialize($language);

//retrieve the object

if(isset($_SESSION['language_object'])) {
			$language_obj = unserialize($_SESSION['language_object']);
			$this->language_array = $language_obj->get_array();
			return;
		}

 

BTW, you must have to include the class definition when you want to retrieve object from the session.

Hope this will help you.

Thanks

tapos,

 

Ok the serialize and unserialize worked like champ. Thanks very much.

 

I don't follow/understand what you mean by:

 

"BTW, you must have to include the class definition when you want to retrieve object from the session."

 

Also, what is the exact reason I have to serialize and object before storing in a session? I never read this anywhere.

"BTW, you must have to include the class definition when you want to retrieve object from the session."

that means your object is a instance of Language, so when u retrieve the object from  session so, Language class definition must be available there.

 

Also, what is the exact reason I have to serialize and object before storing in a session? I never read this anywhere.

how u will save a binary data into a file?  u must have to it in text format, thats why u need to serialize.

use session_register function to save the object in the session, it automatically done that serialize and unserialize thing.

see this http://www.php.net/manual/en/language.oop.serialization.php

 

 

 

Ok makes sense tapos, I think I will stick with using serialize() and unserialize() versus using session_register because of the following:

 

"If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled."

 

Thanks for the help, very helpful.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.