摘要:namespace app\index\controller; class Singleton{ private $link;//数据库连接 private static $instance = null;//保存类实例的静态成员
namespace app\index\controller;
class Singleton{
private $link;//数据库连接
private static $instance = null;//保存类实例的静态成员变量
// 连接数据库(私有化构造方法)
private function __construct($host,$username,$passwoord){
$this->link = mysqli_connect($host,$username,$passwoord);
$this->query("SET NAMES 'utf8'",$this->link);
return $this->link;
}
//(私有化克隆方法)
private function __clone(){}
//单例方法,用于访问实例的公共静态方法
public static function getInstance($host,$username,$passwoord){
if(is_null(static::$instance)){
static::$instance = new static($host,$username,$passwoord);
}
return static::$instance;
}
}
$db = Singleton::getInstance('127.0.0.1','root','123456');
批改老师:查无此人批改时间:2019-01-18 17:07:26
老师总结:作业完成的不错,代码很整洁。少年,继续加油。