PHP中的单例模式

来源:互联网 发布:怎么利用淘宝赚钱 编辑:程序博客网 时间:2024/05/16 01:15
 class Singleton{ //变量为私有、静态、初始化为nullprivate static $_instance = null; // 防止直接使用new ClassName()去获得实例public function __construct(){trigger_error('New ClassName() is not allow!Please use static function getInstance() instead!',E_USER_ERROR);}// 防止对象被复制克隆public function __clone(){trigger_error('Clone is not allow!',E_USER_ERROR);}public static function getInstance(){if(self::$_instance instanceof self){//没有则初始化 self::$_instance = new self; } return self::$_instance; }}

原创粉丝点击