获取 PHP 中类的当前实例
我如何访问已在 PHP 中运行的类的当前实例?
在我的一个 PHP 类中,我想调用另一个已经作为服务器运行的函数。所以我无法创建新实例。
谢谢。
帕
how do i get access to a current instance of class that's already running in PHP?
in my one PHP class, I want to call a function from another that's already running as a server. so i cant create a new instance.
thanks.
p a
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想要的叫做单例。
使用 Singleton,您只能在服务器上运行您的类的一个实例。
为此,您必须:
static
字段存储在类中private
构造函数,这样您就无法自己创建static
的 实例code> 方法,调用构造函数一次,并返回唯一实例资源:
同一主题:
What you want is called a singleton.
With a Singleton you can have only one instance of your class running on the server.
To do so you must :
static
fieldprivate
constructor so you can't create instances yourselfstatic
method which call the constructor once, and return the only instanceResources :
On the same topic :