数据库连接的单例模式

原创 2019-02-14 00:05:17 304
摘要:class DBcontent{ //将构造方法私有化:禁止从外部实例化类 private function __construct() {} //将克隆方法私有化:禁止从外部克隆该类的实例 private function __clone() {} //创建内部属性$instance,用来保存当前类实例 protected static $instance = null; //创建外部接口,

class DBcontent

{


//将构造方法私有化:禁止从外部实例化类

private function __construct() {}


//将克隆方法私有化:禁止从外部克隆该类的实例

private function __clone() {}




//创建内部属性$instance,用来保存当前类实例

protected static $instance = null;


//创建外部接口,用来返回当前类的唯一实例

public static function getInstance($host=null,$db=null,$user=null,$pwd=null)

{

if (is_null(static::$instance)) {

static::$instance = new static();

}


return static::$instance;

}

}


$DB=DBcontent::getInstance('127.0.0.1','test','root','root');


批改老师:韦小宝批改时间:2019-02-14 09:13:45
老师总结:写的很不错 设计模式要使用到实际的项目中才可以快速记忆和掌握!

发布手记

热门词条