是否在请求之间共享PHP单例类?

编程入门 行业动态 更新时间:2024-10-07 19:21:14
本文介绍了是否在请求之间共享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单例类?

本文发布于:2023-11-12 14:26:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581738.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   单例类

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!