开发者

get current instance of class in PHP

开发者 https://www.devze.com 2023-01-16 06:27 出处:网络
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 functi开发者_Python百科on from another that\'s already running as a server. so

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 functi开发者_Python百科on from another that's already running as a server. so i cant create a new instance.

thanks.

p a


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 :

  • Wikipedia - Singleton pattern
  • php.net - singleton

On the same topic :

  • Creating the Singleton design pattern in PHP5
  • What is so bad about Singletons
0

精彩评论

暂无评论...
验证码 换一张
取 消