在PHP 5中創建單例設計模式如何使用PHP 5類創建Singleton類?
在PHP 5中創建單例設計模式
慕桂英4014372
2019-06-24 10:44:40
TA貢獻1869條經驗 獲得超4個贊
class Singleton{ protected static $instance = null; protected function __construct() { //Thou shalt not construct that which is unconstructable! } protected function __clone() { //Me not like clones! Me smash clones! } public static function getInstance() { if (!isset(static::$instance)) { static::$instance = new static; } return static::$instance; }}
class Foobar extends Singleton {};$foo = Foobar::getInstance();
舉報