Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/143379-solved-oophp-user-login/
Share on other sites

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

}

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.