摘要:<?phpheader ('content-type:text-htnl;chatset=utf8');/** * Created by PhpStorm. * User: guanzhiyu * Date: 2019/4/29 * Time: 10:52 */class DbConnect{ private $pdo; //
header('content-type:text-htnl;chatset=utf8');
/**
* Created by PhpStorm.
* User: guanzhiyu
* Date: 2019/4/29
* Time: 10:52
*/
class DbConnect
{
private $pdo;
//构造方法私有化
private function __construct()
{
// $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
// static ::$pdo = $pdo;
}
//克隆方法私有化
private function __clone()
{
// TODO: Implement __clone() method.
}
protected static $instance = null;
//创建一个外部接口,创建并返回当前类的唯一实例
public static function getInstance()
{
if(is_null(static::$instance)){
static::$instance = new static();
}
return static::$instance;
}
public function getDb(){
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
return $pdo;
}
}
$pdo = DbConnect::getInstance();
var_dump($pdo->getDb());
echo '
';
$pdo1 = DbConnect::getInstance();
var_dump($pdo1->getDb());
批改老师:欧阳批改时间:2019-04-30 09:14:37
老师总结:完成的不错。设计模式还有不少,要多看看,这样成长的才快。继续加油。