I'm trying to achieve the following goal:
Using this general singleton class:
abstract class Singleton {
private static $instance = null;
public static function self()
{
if(self::$instance == null)
{
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
}
I'd love to be able to create Singleton concrete class such as:
class Registry extends Singleton {
private function __construct() {}
...
}
and then use them as:
Registry::self()->myAwesomePonyRelatedMethod();
But obliviously __CLASS__
is intended as Singleton
so a fatal error occurs about PHP not being able to instantiate an abstract class. But the truth is that I want Registry (for example) to be instantiated.
So I tried with get_class($this)
but being a static class, Singleton has no $this.
What could I do to make it work?