Posted by Adrián Ribao Martínez on 12/01/06 22:00
Thank you very much!
Michael Fesser wrote:
> .oO(Adrián Ribao Martínez)
>
>>Hello, I have to create an instance of a class that should be accessible
>>from all the other classes.
>>What do you recommend me?
>>- Using globals?
>>- Pass the instance as an argument and load it in a __constructor?
>>- Something different?
>
> A singleton, e.g. (assuming PHP5)
>
> class TMyClass {
> private static $instance = NULL;
>
> public static function getInstance() {
> if (!isset(self::$instance)) {
> self::$instance = new self();
> }
> return self::$instance;
> }
>
> ...
> }
>
> Then wherever you need that instance, you just have to get it:
>
> $foo = TMyClass::getInstance();
>
> HTH
> Micha
Navigation:
[Reply to this message]
|