jonniejoejonson Posted September 11, 2009 Share Posted September 11, 2009 If you were creating a social network like facebook...would you create a user class and then when a user logsin, store the user object the they create in a session?.. is this the correct use of classes? and would you store this object in a session?... regards to any responders.. j Quote Link to comment https://forums.phpfreaks.com/topic/173893-when-to-oop/ Share on other sites More sharing options...
rhodesa Posted September 11, 2009 Share Posted September 11, 2009 yes, that is what i would do Quote Link to comment https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-916710 Share on other sites More sharing options...
PFMaBiSmAd Posted September 11, 2009 Share Posted September 11, 2009 By "store this object in a session" I assume you mean create the object as a session variable - <?php require 'your_class.php'; session_start(); if(!isset($_SESSION['user_object_name'])){ // create instance $_SESSION['user_object_name'] = new your_class(); echo 'object created<br />'; } else { echo 'object exists<br />'; } $_SESSION['user_object_name']->class_function(); // example call to class function echo $_SESSION['user_object_name']->class_variable; // example access of class variable ?> Quote Link to comment https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-916713 Share on other sites More sharing options...
ignace Posted September 11, 2009 Share Posted September 11, 2009 Don't forget that when you use custom objects in sessions you need to load them before session_start on each request otherwise it won't be able to re-create the custom object as it can't find the class definition Quote Link to comment https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-916714 Share on other sites More sharing options...
kratsg Posted September 11, 2009 Share Posted September 11, 2009 Depending on how I use the class... I may just store a value in the session instead from which I can initiate the class again on the next page and recreate the object. Say my class dealt with file manipulation.. perhaps I just initiate the class with a filename and it gives me all I need from it... so i would just store the filename in the session to call the class again when I need it... Quote Link to comment https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-916721 Share on other sites More sharing options...
ToonMariner Posted September 11, 2009 Share Posted September 11, 2009 Don't forget that when you use custom objects in sessions you need to load them before session_start on each request otherwise it won't be able to re-create the custom object as it can't find the class definition If you use __autoload this is not an issue. storing an object in a session means the object gets auto-magically serialized and un-serialized - you can also control that using the __sleep and __wake methods within the class itself. Quote Link to comment https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-916732 Share on other sites More sharing options...
ignace Posted September 12, 2009 Share Posted September 12, 2009 Don't forget that when you use custom objects in sessions you need to load them before session_start on each request otherwise it won't be able to re-create the custom object as it can't find the class definition If you use __autoload this is not an issue. storing an object in a session means the object gets auto-magically serialized and un-serialized - you can also control that using the __sleep and __wake methods within the class itself. That is if you load it (declare it) before session_start() in both cases. Like I said here: Don't forget that when you use custom objects in sessions you need to load them before session_start on each request otherwise it won't be able to re-create the custom object as it can't find the class definition Quote Link to comment https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-917098 Share on other sites More sharing options...
Daniel0 Posted September 12, 2009 Share Posted September 12, 2009 Don't store the object in the sessions. Store a reference to it (e.g user id) and instantiate the object again. Otherwise it might be difficult ensuring that you have up-to-date information in the object. Quote Link to comment https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-917227 Share on other sites More sharing options...
jonniejoejonson Posted September 14, 2009 Author Share Posted September 14, 2009 Thanks for all the responses.. however it would appear there are a few conflicting responses.. i would therefore conclude that it is dependendant on the information that i am storing. Thanks again J. Quote Link to comment https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-918216 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.