16

I am using the singleton pattern in all my PHP class files.

Would it by any chance cause a user's action on the site to conflict with other user's action?

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), since singleton prevents multiple instance of a class and returns just a single instance.

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.

Valerio Bozz
  • 1,176
  • 16
  • 32
War Coder
  • 454
  • 1
  • 7
  • 24
  • Generally when you have "what if" questions like this, you should assume that it is incorrect unless proven otherwise. If you even have to ask here and can't just go run it yourself and verify that it is correct, then you have some deep flaws in your build that need to be factored out. Asking code to prove to you that it does what is intended should not be a painful effort, and when it is, you have much bigger problems that will persist into hundreds or thousands of tiny, seemingly unrelated problems until you sort out the root ones. – mopsyd Oct 27 '17 at 03:04
  • In my own opinion, there is rarely any circumstance that warrants a singleton at all. Using them as a universal approach is going to make your code both untestable and unscalable, and will make your end of life date significantly sooner than neccessary. However I also disagree with the mentality that there is never a valid reason for a singleton. 99% of the time, there is not a reason for one, and it indicates either laziness or unsound planning. The remaining 1% of the time it is filling the exact job it is intended for and should not change, no matter how much people whine about them. – mopsyd Oct 27 '17 at 03:07
  • It should generally be recognized that singletons used poorly are nearly always a response to self inflicted problems. There is no reason to say "can't make another instance" when "I didn't make another instance" provides the same solution with no limitation. An effective use of a singleton for example would be creating a prototyper, which needs to maintain a single blank instantiated instance to clone from, which is then initialized manually through a method other than the constructor. That's really about the only time I use them, but I do consistently use them for that. – mopsyd Oct 27 '17 at 03:11
  • In the above example, it should also be stated that the actual prototyper IS NOT a singleton. Only the blank template baseline object instances that it produces from are. The prototyper itself can be reset or created as another parallel instance, and in the process release/renew/recieve an entirely unrelated baseline set as circumstance warrants. – mopsyd Oct 27 '17 at 03:16

4 Answers4

33

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
  • 1,292
  • 9
  • 7
28

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
  • 86,251
  • 24
  • 115
  • 132
  • so what are its obvious disadvantages? – War Coder Jan 11 '09 at 02:33
  • 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 Jan 11 '09 at 02:44
  • 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 Jan 12 '09 at 03:45
5

The Singleton pattern is one of the more controversial patterns. Critics argue that Singletons introduce Global State into an application and tightly couple the Singleton and its consuming classes. This leads to hidden dependencies and unexpected side-effects, which in turn leads to code that is harder to test and maintain.

Critics further argue that it is pointless to use a Singleton in a Shared Nothing Architecture like PHP where objects are unique within the Request only anyways. It is easier and cleaner to create collaborator object graphs by using Builders and Factory patterns once at the beginning of the Request.

Singletons also violate several of the "SOLID" OOP design principles and the Law of Demeter. Singletons cannot be serialized. They cannot be subtyped (before PHP 5.3) and won't be Garbage Collected because of the instance being stored as a static attribute of the Singleton.

shaikh
  • 1,355
  • 4
  • 16
  • 30
2

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.

Joey Guerra
  • 596
  • 6
  • 11