首页 > 技术文章 > 单例模式复习

qt0909 2019-04-09 14:20 原文


1
/** 2 * 单例模式 3 */ 4 class Single 5 { 6 protected static $ins = null; 7 8 if(self::$ins === null) 9 { 10 self::$ins = new Single(); 11 } 12 13 return self::$ins; 14 15 //防止外部调用并且防止继承后重写 16 final protected function __construct(){} 17 18 //防止外部克隆并且防止继承后克隆 19 final protected function __clone(){} 20 21 }

使用场景:数据库类,一般的小中型项目就一台mysql服务器想连接数据库的话就用一个对象去连接就好了。

推荐阅读