premiso Posted March 5, 2009 Share Posted March 5, 2009 Hey, Just curious on storing Objects in Session with PHP. I know .NET does this regularly and was wondering if anyone had any reason why this would be avoided. Basically I am going to have a Person Class that extends Address. A person can have 1 or more addresses. I want to store this person class in an object in session when they are modifying their data (UserCP). So instead of having to re-query each time, I just store that in session and make an isDirty variable so when they are done, it checks that variable. If anything has been changed it updates the database. I do not really see anything wrong with this, but given that PHP's OOP is still getting the tweaks worked out I figured I would see if anyone had any thoughts. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/148128-solved-oop-session-and-class-objects/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2009 Share Posted March 5, 2009 This has worked forever (only requirement is the class definition must exist before session_start() so that the data can be restored) - <?php require 'your_class.php'; session_start(); if(!isset($_SESSION['some_object_name'])){ // create instance $_SESSION['some_object_name'] = new your_class(); echo 'object created<br />'; } else { echo 'object exists<br />'; } $_SESSION['some_object_name']->class_function(); echo $_SESSION['some_object_name']->class_variable; ?> Quote Link to comment https://forums.phpfreaks.com/topic/148128-solved-oop-session-and-class-objects/#findComment-777569 Share on other sites More sharing options...
Mchl Posted March 5, 2009 Share Posted March 5, 2009 Or it has to be loadable by autoload functionality. Quote Link to comment https://forums.phpfreaks.com/topic/148128-solved-oop-session-and-class-objects/#findComment-777579 Share on other sites More sharing options...
premiso Posted March 5, 2009 Author Share Posted March 5, 2009 This has worked forever (only requirement is the class definition must exist before session_start()... That must be the kicker that I never got to work. Thanks for that PFMaBiSmAd. Yea, I have seen the autoload, I have not looked into it. But I will and see if that works. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/148128-solved-oop-session-and-class-objects/#findComment-777587 Share on other sites More sharing options...
ScotDiddle Posted March 5, 2009 Share Posted March 5, 2009 This has worked forever (only requirement is the class definition must exist before session_start() so that the data can be restored) As a follow-up question, what about us folks who start sessions automatically in the ini file ? For my Oracle Object, I get "Incomplete_Object" because of it... It doesn't seem to be causing my application any harm... Is it "safe" to continue using an incomplete object ? What are the ramifications ? Thanks, Scot L. Diddle, Richmond VA Quote Link to comment https://forums.phpfreaks.com/topic/148128-solved-oop-session-and-class-objects/#findComment-777588 Share on other sites More sharing options...
premiso Posted March 5, 2009 Author Share Posted March 5, 2009 This has worked forever (only requirement is the class definition must exist before session_start() so that the data can be restored) As a follow-up question, what about us folks who start sessions automatically in the ini file ? For my Oracle Object, I get "Incomplete_Object" because of it... It doesn't seem to be causing my application any harm... Is it "safe" to continue using an incomplete object ? What are the ramifications ? Thanks, Scot L. Diddle, Richmond VA I would use the autoloader as pointed out. As far as the incomplete object, I do not think it is kosher. If that does not answer your question, open up a new topic please. Topic Solved. Quote Link to comment https://forums.phpfreaks.com/topic/148128-solved-oop-session-and-class-objects/#findComment-777594 Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2009 Share Posted March 5, 2009 If your session is automatically started in php.ini, I believe you are out of luck using the direct ($_SEESION[...] = new class() method shown. You would need to fall back to use an indirect method of storing and retrieving the object variables in the session. Quote Link to comment https://forums.phpfreaks.com/topic/148128-solved-oop-session-and-class-objects/#findComment-777604 Share on other sites More sharing options...
premiso Posted March 6, 2009 Author Share Posted March 6, 2009 If your session is automatically started in php.ini, The best thing is, I have dedicated hosting, so full access to the php.ini. I will make sure that is set to be off. Quote Link to comment https://forums.phpfreaks.com/topic/148128-solved-oop-session-and-class-objects/#findComment-777837 Share on other sites More sharing options...
SieRobin Posted March 6, 2009 Share Posted March 6, 2009 Why don't you just serialize and unserialize your object? It will save all of your objects variables. Quote Link to comment https://forums.phpfreaks.com/topic/148128-solved-oop-session-and-class-objects/#findComment-777933 Share on other sites More sharing options...
RussellReal Posted March 6, 2009 Share Posted March 6, 2009 Why don't you just serialize and unserialize your object? It will save all of your objects variables. ahaha thats what I was gonna say!!! Quote Link to comment https://forums.phpfreaks.com/topic/148128-solved-oop-session-and-class-objects/#findComment-777987 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.