duniya Posted August 17, 2007 Share Posted August 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Buyocat Posted August 17, 2007 Share Posted August 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
duniya Posted August 18, 2007 Author Share Posted August 18, 2007 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. Quote Link to comment Share on other sites More sharing options...
duniya Posted August 18, 2007 Author Share Posted August 18, 2007 I have decided to use the singleton pattern when implementing db connection. Make more sense now after reading http://www.tozz.org/php/simple-database-connection-using-the-singleton-pattern-in-php Quote Link to comment 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.