views:

816

answers:

3
+1  Q: 

Singleton in PHP

am working on a web app in PHP that requires users to register and login with their credentials. However, i am using the singleton pattern in all my PHP class files.

I have something bothering my mind that i would like to clarify. For instance, When the application goes live and we have several users on the site at the same time, doing similar things hereby calling the same php classes (behind the scene). Now, since singleton prevents multiple instance of a class and returns just a single instance. Would it by any chance cause a user's action on the site to conflict with other user's action.

e.g I have a class called Search.php and it is a singleton class. This class handles all search queries from the website. If several users are performing a search on the site at the same time, will their actions conflict with each other since its just a single instance of the Search class that can be created.

Thanks very much for your time.

+14  A: 

Each request is self contained and does not share data with other requests (unless you use specific extensions for that, like memcache). So having singletons in your application would not affect separate requests from separate users.

What should be of concern to you is your overuse of the singleton pattern. A singleton is an OO version of a global, and can cause some weird bugs if you are not careful. It is better to use scoped operations that do not rely on global settings, and use singletons sparingly.

Eran Galperin
so what are its obvious disadvantages?
War Coder
As I said the same as a global - by relying on information that can be set from anywhere in the application, you have no knowledge on what changed it last. Could lead to unpredictable results.
Eran Galperin
You could try something like Zend_Registry to store objects/values instead of making many classes into singletons. It should be easy to implement something similar by yourself, or just use that code. http://framework.zend.com/manual/en/zend.registry.html
OIS
thanks very much, i will take a look at that now.
War Coder
+6  A: 

The short answer is no.

Each page request is handled as a unique instance and the only thing that tie them together for each user is the session cookie. Try to think of PHP as an application which starts when you call a script and dies when the script finishes. It does not maintain any state and is not inherently aware of any other PHP instances.

The singleton pattern is simply a way for you to design a class, where you can call it anywhere in your code (like a global) without having to care about whether it already has been instantiated or not and you want it to persist in memory.

Hans
A: 

I agree, the answer is no. I'm thinking about the Singleton pattern in PHP right now and have decided that although the Singleton pattern can be coded in PHP, it is not really implemented since the instance is not shared across requests (or stored in the process memory which is the case for web server environments like ASP.net, Java and Ruby on Rails?) You can serialize the Singleton instance and store it in session, but still, it's not shared across sessions. I speculate that it would have to be stored in cache in order to fully implement the Singleton pattern in PHP. But I haven't done that yet, so I'm not certain.

jg