Jump to content

Singleton DB design pattern


aharvilla

Recommended Posts

I am having trouble understanding the singleton design pattern in order to setup a DB object. My problem is that I dont think I am trying to use it in the correct way which might have answered my problem to begin with. Even so, I still want to know if it is possible when starting my page to 1. instantiate the object and then 2. Use that instantiated DB object by OTHER objects instantiated after in some way. I apologize if I might be forgetting something but from what I understand, this is what my idea of the singleton design pattern. Would i have to pass the Object to a class or a function as a parameter or something like that or can i access the object directly from another class without having to pass any kind of anything.

Link to comment
Share on other sites

I am having trouble understanding the singleton design pattern in order to setup a DB object. My problem is that I dont think I am trying to use it in the correct way which might have answered my problem to begin with. Even so, I still want to know if it is possible when starting my page to 1. instantiate the object and then 2. Use that instantiated DB object by other objects in some way. I apologize if I might be forgetting something but from what I understand, this is what my idea of the singleton design pattern. Would i have to pass the Object to a class or a function as a parameter or something like that or can i access the object directly from another class without having to pass any kind of anything?

 

You'd have to access it via its static getInstance() method.  Something like:

 

$db = Database::getInstance();
// do stuff with $db

 

Remember: with a Singleton, getInstance either instantiates the db and returns that new instance OR returns the existing instance.  To get your hands on a Singleton's cookies, you need to call its static method.

Link to comment
Share on other sites

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.