JustinK101 Posted June 6, 2008 Share Posted June 6, 2008 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 More sharing options...
tapos Posted June 6, 2008 Share Posted June 6, 2008 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 Link to comment https://forums.phpfreaks.com/topic/108941-storing-an-object-in-a-session/#findComment-558921 Share on other sites More sharing options...
JustinK101 Posted June 6, 2008 Author Share Posted June 6, 2008 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. Link to comment https://forums.phpfreaks.com/topic/108941-storing-an-object-in-a-session/#findComment-558926 Share on other sites More sharing options...
tapos Posted June 6, 2008 Share Posted June 6, 2008 "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 Link to comment https://forums.phpfreaks.com/topic/108941-storing-an-object-in-a-session/#findComment-558940 Share on other sites More sharing options...
JustinK101 Posted June 6, 2008 Author Share Posted June 6, 2008 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. Link to comment https://forums.phpfreaks.com/topic/108941-storing-an-object-in-a-session/#findComment-558947 Share on other sites More sharing options...
PFMaBiSmAd Posted June 6, 2008 Share Posted June 6, 2008 If you create the object directly in the session, you can avoid all the extra steps - $_SESSION['language_object'] = new your_language_class; Link to comment https://forums.phpfreaks.com/topic/108941-storing-an-object-in-a-session/#findComment-558948 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.