摘要:将构造方法私有化:禁止从外部实例化类private function __construct() {}将克隆方法私有化:禁止从外部克隆该类的实例private function __clone() {}创建内部属性$instance,用来保存当前类实例protected static $instance = null;创建外部接口,用来返回当前类的唯一实例 public static functi
// 将构造方法私有化:禁止从外部实例化类 private function __construct() {} // 将克隆方法私有化:禁止从外部克隆该类的实例 private function __clone() {} // 创建内部属性$instance,用来保存当前类实例 protected static $instance = null; // 创建外部接口,用来返回当前类的唯一实例 public static function getInstance() { if (is_null(static::$instance)) { static::$instance = new static(); } return static::$instance; }
批改老师:韦小宝批改时间:2018-11-02 11:15:23
老师总结:代码写的没有啥问题!但是是不是还可以使用单例模式来完成个小案例来作为练手啊!这样不仅仅可以加深我们对设计模式的印象,还可以更快的掌握设计模式的思想!偷偷告诉你强迫自己用设计模式来写代码比其他人进步的要