是否在请求之间共享PHP单例类? [英] Are PHP singleton classes somehow shared between requests?

查看:48
本文介绍了是否在请求之间共享PHP单例类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在所有PHP类文件中使用单例模式。

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?

例如,当应用程序上线并且我们同时在站点上有多个用户时,通过调用相同的PHP类(在幕后)来做类似的事情。 ,因为单例阻止一个类的多个实例并仅返回一个实例。

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.

例如我有一个名为Search.php的类,它是一个单例类。此类处理来自网站的所有搜索查询。如果多个用户同时在站点上执行搜索,由于它们只是可以创建的Search类的单个实例,因此他们的操作会相互冲突。

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.

非常感谢您的时间。

推荐答案

简短的回答是否。

每个页面请求都作为一个唯一实例处理,并且将每个用户的唯一请求绑定为会话cookie。尝试将PHP视为一个应用程序,该应用程序在调用脚本时启动,并在脚本结束时终止。它不维护任何状态,也不固有地知道其他任何PHP实例。

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.

这篇关于是否在请求之间共享PHP单例类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆