Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are PHP singleton classes somehow shared between requests?

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.

like image 784
War Coder Avatar asked Jan 11 '09 02:01

War Coder


People also ask

Is a singleton shared?

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.

Can singleton class inherit another class?

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.

Can singleton have multiple instances?

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.

When we should not use singleton pattern?

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.


4 Answers

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.

like image 133
Hans Avatar answered Oct 23 '22 09:10

Hans


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.

like image 42
Eran Galperin Avatar answered Oct 23 '22 11:10

Eran Galperin


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.

like image 5
shaikh Avatar answered Oct 23 '22 10:10

shaikh


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.

like image 2
Joey Guerra Avatar answered Oct 23 '22 11:10

Joey Guerra