1

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

p a
  • 11
  • 2
  • 3
    I don't understand, especially the part about running a class as a server. Can you show some code? – Pekka Sep 16 '10 at 21:16
  • hi pekka the code is really complicated there's a lot of classes. but basically its a websocket server written in PHP that you can find here: http://github.com/nicokaiser/php-websocket. I am basically trying to call one of the functions in this server from the outside (for example, if a user clicks a button, call send data function inside the server). – p a Sep 16 '10 at 21:33

1 Answers1

3

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 :

Community
  • 1
  • 1
Colin Hebert
  • 85,401
  • 13
  • 150
  • 145