Jump to content

[SOLVED] Why implement the singleton pattern in php?


duniya

Recommended Posts

Hi,

 

I want to know why implement the singleton pattern in php?

 

I am trying to work out if there is any real benefit in using this pattern in PHP or if it is just an unnecessary overhead.

 

I have searched on google and read posts on several forums but I am still having trouble fully understanding the real benefits.

 

The most common example used to explain the singleton pattern in PHP is database connection. Like the example in the PHP documentation about the singleton pattern.  It explains, " Implementing this pattern allows a programmer to make this single instance easily accessible by many other objects".

 

I kind of understand why this would be useful in c++ or java  but I really cant understand why it would be useful in a scripting language such as PHP.  From what I gather in PHP each request creates a new PHP environment on the server i.e each request is a fresh request and as no knowledge of any other requests.  However, with C++ and Java application has common memory which can remember between requests etc...

 

Going back to the common example of db connection being implemented as a singleton pattern.  Isnt it true by calling mysql_connect()  with the same arguments will used the last database connection anyway.

 

I would appreciate your thoughts.

 

Best regards.

 

 

 

 

 

 

 

Link to comment
Share on other sites

I will give an example from a project I'm working on right now.  There's a config file which must be opened and then parsed to convert the config information into a hash table.  The config information is used at several points in the application.  I could read the config information at each point which would require opening the file and parsing it each time, or I could maintain a singleton of the config data object and access the information through the singleton.  The result is I only have to read the file in once per a request.

Link to comment
Share on other sites

Thanks. That makes sense in your situation.  However, I am still not sure why would you implement a database connection has a singleton.  Basically, I am building a web application and evaluating whether to use the singleton pattern to make database connection.

 

Maybe using the singleton pattern for db connection I would not have to enter db connection details all over the place.  Though I  could overcome this by including db.inc.php file which contains connection details and then include this file to all other files.

 

Best regards.

 

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.