使用单例模式封装PDO链接数据库
1、封装php Single 类
<?php
class Single
{
private static $_instance = null;
private function __construct(){}
private function __clone(){}
public static function getInstance()
{
if (self::$_instance === null) {
self::$_instance = new Single();
}
return self::$_instance;
}
public function hehe($dbName){
return new PDO('mysql:host=localhost;dbname='.$dbName.';', 'root', 'root');
}
}
?>
2、引用
<?php
require 'Single.class.php';
$db = Single::getInstance();
$pdo=$db->hehe('DBname');
$data=$pdo->query('desc tableName'/sql)->fetch();
var_dump($data);
die;
$db->destruct();
?>