Posted by Michael Fesser on 12/01/06 15:23
..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]
|