Singleton在PHP [英] Singleton in PHP

查看:92
本文介绍了Singleton在PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP中的网络应用程序,需要用户注册并使用其凭据登录。但是,我使用单例模式在我所有的PHP类文件。

am working on a web app in PHP that requires users to register and login with their credentials. However, i am using the singleton pattern in all my PHP class files.

我有一些困扰我的想法,我想澄清。例如,当应用程序运行时,我们在网站上有多个用户同时,做类似的事情,从而调用相同的php类(幕后)。现在,因为单例阻止了一个类的多个实例,并且只返回一个实例。是否会导致用户在网站上的操作与其他用户的操作冲突。

I have something bothering my mind that i would like to clarify. 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). Now, since singleton prevents multiple instance of a class and returns just a single instance. Would it by any chance cause a user's action on the site to conflict with other user's action.

例如我有一个名为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.

这篇关于Singleton在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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