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.
Singletons are objects that should only ever be created once, then shared everywhere they need to be used. They are common on Apple's platforms: FileManager , UserDefaults , UIApplication , and UIAccelerometer are all mostly used through their singleton implementations.
You cannot inherit or instantiate a static class. Unlike static classes, Singleton classes can be inherited, can have base class, can be serialized and can implement interfaces. You can implement Dispose method in your Singleton class. So, static classes are much less flexible compared to Singleton classes.
The Singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields.
One rather bad thing about singletons is that you can't extend them very easily. You basically have to build in some kind of decorator pattern or some such thing if you want to change their behavior.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With