php设计模式
发布时间:2022-09-28 07:30:16 323
相关标签: # html# php# mysql# sql# 信息
参考:https://designpatternsphp.readthedocs.io/zh_CN/latest/README.html#
数据库单例模式代码:
//先建立单模:三私(私有变量、私有构造函数、私有的__clone函数)一公(公有方法),再封装连接数据库,以及返回结果
class MySQLDB{
//定义连接数据库需要用到的私有属性
private $host; //主机id
private $port; //端口号
private $user; //用户
private $psw; //密码
private $dbname; //数据库名称
private $charset; //字符集
//定义静态变量保存当前类的实例
private static $instance;
//防止在外部实例化
private function __construct($config){
$this -> initParam($config);//初始化参数-----实例化时自调用
$this -> initConn();//连接数据库-------因为已经初始化了,现在所有参数已经是我们需要的参数了,所以不需要再带参数了
$this -> initDB();//选择数据库
$this -> initCharset();//选择字符集
}
//防止在外部克隆
private function __clone(){
}
//通过静态公有的方法获取这个类的实例
public static function getInstance($config){
//当前对象不属于当前例就实例化,也就是静态变量在当前类中只能实例化一次,若是第一次实例化就实例化,若第二次实例化就返回一个当前的实例值。
if (!self::$instance instanceof self) {
self::$instance = new self($config);
}
return self::$instance;
}
//初始化成员变量
private function initParam($config){
$this -> host = isset($config['host']) ? $config['host'] : 'localhost';
$this -> port = isset($config['port']) ? $config['port'] : 3306;
$this -> user = isset($config['user']) ? $config['user'] : 'root';
$this -> psw = isset($config['psw']) ? $config['psw'] : '';
$this -> dbname = isset($config['dbname']) ? $config['dbname'] : '';
$this -> charset = isset($config['charset']) ? $config['charset'] : 'utf8';
}
//连接数据库
private function initConn(){
$link = mysql_connect("{$this -> host}:{$this -> port}" , $this->user ,$this->psw);
//判断数据库连接是否成功
if (!$link) {
echo "mysql连接错误,错误原因如下:
";
echo "错误代码:".mysql_errno().'
';
echo "错误信息:".mysql_error();
exit;
}
}
//选择数据库
private function initDB(){
//mysql_select_db('{$this -> dbname}');
$this -> query("use `{$this -> dbname}`");
}
//选择字符集
private function initCharset(){
//mysql_query('set names `{$this -> charset}`');
$this->query("set names {$this->charset}");
}
//封装一个执行SQL语句的方法
/**
* @param $sql string 执行的SQL语句
* @return $result 如果是数据查询语句返回结果集,如果是数据操作语句返回true,false
*/
private function query($sql){
if (!$result = mysql_query($sql)) {
echo 'SQL语句执行失败。
';
echo '错误号是:'.mysql_errno().'
';
echo '错误信息:'.mysql_error().'
';
echo '错误的SQL语句是:'.$sql.'
';
exit;
}
return $result;
}
/**
* 获得所有的数据
* @param $sql string 执行SQL
* @param $type string 匹配的类型 assoc||array||row
* @return $result array 返回值为二维数组
*/
public function fetchAll($sql,$type = 'assoc'){
$res = $this -> query($sql); //返回的是资源的结果集
//定义一个数组来保存允许匹配的类型
$allow_array = array('assoc','array','row');
if (!in_array($type, $allow_array)) {
$type = 'assoc';
}
$fn = 'mysql_fetch_'.$type;
//将一条结果转成二维数组
$result = array();
while ($rows = $fn($res)) {
$result[] = $rows;
}
return $result;
}
/**
* * 获取一条数据
* @param $sql string 执行SQL
* @param $type string 匹配的类型 assoc||array||row
* @return $result array 返回值为一维数组
*/
public function fetchRow($sql,$type = 'assoc'){
$result = $this -> fetchAll($sql,$type);
if(!empty($result)){
return $result[0];//返回第一行
}
return $result;
}
/**
* 获取一行一列
* @param $sql string 执行SQL
* @param $type string 匹配的类型 assoc||array||row
* @return $result string 返回值为一行一列
*/
public function fetchColumn($sql){
$result=$this->fetchRow($sql,'row');
if(!empty($result)){
return $result[0]; //返回一行一列
}
return $result;
}
}
//设置响应头,防止乱码
header('Content-type:text/html;charset=utf-8');
$config = array(
'host' => 'localhost',
'port' => 3306,
'user' => 'root',
'psw' => 'mysql',
'dbname' => 'oop',
'charset' => 'utf8'
);
$db = MySQLDB::getInstance($config);//获取对象的实例
$rs = $db -> fetchAll('select * from star');//自己选择要执行的SQL语句
echo '';
var_dump($rs);
文章来源: https://blog.51cto.com/u_12286670/5379040
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报