0

我最近发现单身人士很糟糕。我找不到答案如何将我当前的代码重新创建为没有单例。

我现在拥有的:

class checker {
   private static $inst;
   public static function getInst() {
    if(!isset(self::$inst))
        throw new Exception("Error 13: Check module not started.");
    else
        return self::$inst;
   }

   function __construct()   {

    if(!isset(self::$inst))
        self::$inst = $this;
    else {
        throw new Exception("Error 10: Checker not ready?");
        exit;
    }
   }
}

然后我用

checker::getInst()->blaBlaFunction();

我需要它在多个类中,并且只有一个实例。

4

0 回答 0