<?phpnamespace lib;abstract class Singleton{ //設置一個私有的靜態屬性做爲中間變量 private static $instancePool = []; //將構造及克隆私有化,防止經過克隆等方式進行調用 final private function __construct(){ $this->init(); } final private function __clone(){} protected function init() {} final public static function getInstance() { $class = get_called_class(); //若是,所設置的靜態屬性的值,還爲null 的話,那麼就須要實例化本身,不然直接返回 if (!isset(self::$instancePool[$class])) { self::$instancePool[$class] = new static(); } return self::$instancePool[$class]; }}