Reusable Singleton pattern using PHP5

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,
As the %subj% says.. can't get it to work with PHP5. I mean, you coulddo this kind of stuff with templates/generics on other languages andthen make a class singleton by just inheriting from base Singletonclass. No need to redefine GetInstance() (which usually contains thesame redundant kind of code). It should look something like this inPHP5:
class Test extends Singleton{  // no singleton-specific code here}
abstract class Singleton{  private static $instance = null;  private function __construct() {}  private function __clone() {}  public static function GetInstance()  {    if (!isset(self::$instance))      self::$instance = new self; // <-- doesnt really work
    return self::$instance;  }}
The problem is that self refers to Singleton class. I have also beenplaying with __CLASS__ magic constant but it does the same thing.Someone got it working the right way?

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux