获取 PHP 中类的当前实例

发布于 2024-09-20 00:03:44 字数 105 浏览 1 评论 0原文

我如何访问已在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-09-27 00:03:44

你想要的叫做单例。

使用 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 :

  • store your instance in your class as a static field
  • have a private constructor so you can't create instances yourself
  • have a static method which call the constructor once, and return the only instance

Resources :

On the same topic :

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文