Jump to content

Where to Generate Database Connection within Classes


zeodragonzord

Recommended Posts

I'm writing an application with many classes.  Some of these classes interact with MySQL with methods doing specific things like inserting, updating, etc.  I use Pear MDB2.  Where is the best place to generate a MDB2 object?  Should I:

 

  • create the MDB2 object in the constructor and assign it into the object variable to be used in each of its methods
  • create the MDB2 object inside each method
  • create the MDB2 object and pass it into the object in its constructor
  • create the MDB2 object in a config file and use the global keyword to access it in each object

 

I originally had a singleton used inside each method but I don't like the idea that the singleton database connector is global.  Thanks.

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.