摘要:<?php /** * Created by PhpStorm. * User: hello word! * Date: 2019/3/23 * Time: 14:28 */ class demo{
<?php
/**
* Created by PhpStorm.
* User: hello word!
* Date: 2019/3/23
* Time: 14:28
*/
class demo{
//保存类的唯一实例
static protected $instance=null;
//保存连接对象
protected $link;
//私有化构造器
private function __construct($dns,$username,$password)
{
$this->link=new PDO($dns,$username,$password);
}
//私有化克隆
private function __clone()
{
}
//
public static function getInstance($dns,$username,$password){
if(is_null(static::$instance)){
static::$instance = new static($dns,$username,$password);
}
return static::$instance;
}
}
$type='mysql';
$host='127.0.0.1';
$dbname='test';
$charset = 'utf8';
$username='root';
$password='root';
$dns = $type.':host='.$host.';dbname='.$dbname.';charset='.$charset;
$demo = demo::getInstance($dns,$username,$password);
批改老师:查无此人批改时间:2019-03-23 15:17:47
老师总结:完成的不错。单例模式是php多个模式中的一个,要全面学习。继续加油