Are PHP singleton classes somehow shared between r

2019-02-21 12:02发布

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.

4条回答
Rolldiameter
2楼-- · 2019-02-21 12:21

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-02-21 12:22

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.

查看更多
男人必须洒脱
4楼-- · 2019-02-21 12:33

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.

查看更多
Root(大扎)
5楼-- · 2019-02-21 12:47

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.

查看更多
登录 后发表回答