matthewhaworth Posted August 12, 2008 Share Posted August 12, 2008 For a while now I've been creating user objects on every page load and filling them with session data about the user i.e. $_SESSION['user']['name'].. now instead, I want to keep the user details in the object, and pass the object through sessions.. but I'm having a bit of a problem with doing that. Do I check to see if a user object already exists on every page? Or do I check it within the user object's constructor?.. but then again.. I shouldn't have constructed the user object if one already exists.. so in that case, the constructor should never run. So it'll have to check if the user object exists, and if not, create one? Another problem I'm having is, I believe objects don't save data when passed through sessions and that you have to somehow, recreate it through __wakeup.. how would I go about that? Edit: I figure something like <?php if(!is_object($_SESSION['user'])) { $_SESSION['user'] = new user; } ?> Would work? Quote Link to comment https://forums.phpfreaks.com/topic/119384-solved-reinitiating-user-class/ Share on other sites More sharing options...
448191 Posted August 13, 2008 Share Posted August 13, 2008 It would work. May want to put in a isset for good form. You probably don't need __wakeup(). Only when you have a unserializable property value, like a file- or database handle. You shouldn't have that in a "user" object. Another option would be to use a Factory Method in the User class. public static function factory() { if(!isset($_SESSION['user']) || !$_SESSION['user']) { $_SESSION['user'] = new self; } return $_SESSION['user']; } This makes it a Singleton though, so may want to declare the constructor private. Quote Link to comment https://forums.phpfreaks.com/topic/119384-solved-reinitiating-user-class/#findComment-615310 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.