robcrozier Posted February 1, 2009 Share Posted February 1, 2009 Hi all, i bet this type of thing has been asked before, but i hope you can sympathise with my desire to increase my OOPHP skill and help me with this one.. Basically, i've trawled tutorial after tutorial and now i want to try and create a decent user authentication with OOPHP. What i'm struggling with is probably very basic, but here goes. I want to create an instance of a 'user' class on system startup, which will then obviously be pre-loaded by the class' constructor vars. However, how do i only instantiate the class once, and in fact do this on system startup? If include it in my header file, it will try and instanciate the class on every page for example. Is there any way to check if a user object has been created and if so - do not create another one? Any advice on this would be great, hope it makes sense what i'm trying to do! Cheers Quote Link to comment https://forums.phpfreaks.com/topic/143379-solved-oophp-user-login/ Share on other sites More sharing options...
corbin Posted February 1, 2009 Share Posted February 1, 2009 The easiest solution would be a singleton, but I must advise against that. The second easiest solution would be a registry, but that would be overkill unless you plan on having multiple user instances live at once. How exactly do you plan on storing the user class? In a session? In a plain variable? It would basically go like this: if(isset($user_class_instance)) { //it's already made } Quote Link to comment https://forums.phpfreaks.com/topic/143379-solved-oophp-user-login/#findComment-752024 Share on other sites More sharing options...
robcrozier Posted February 1, 2009 Author Share Posted February 1, 2009 Hi corbin, thanks for your reply I'm planning on storing the class as a plain variable. I basically want to create a user class upon system load and then user the various methods of that class to check the users logged-in status and other vars fetched from the database. So if i simply use a check like you suggest that the top of the page: if(!isset($user_class_instance)) { // create instance of user here! } I can create an instance of the user class if there's not one already set. I do just plan to have one instance running at a time as it will basically act as user authentication per session. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/143379-solved-oophp-user-login/#findComment-752033 Share on other sites More sharing options...
corbin Posted February 1, 2009 Share Posted February 1, 2009 From a design point of view, a User class shouldn't be a singleton, but from a practical point of view, it would work fine in this situation. I still advise against it though ;p. My only issue with using a plain variable is that you don't know if it's going to get unset or something. As long as no one else modifies the code, it should be fine though. I'm hoping an OOP person will come along and have a better solution ;p. Quote Link to comment https://forums.phpfreaks.com/topic/143379-solved-oophp-user-login/#findComment-752039 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.