SlideShare une entreprise Scribd logo
1  sur  8
Télécharger pour lire hors ligne
Singleton & Template Method模式

               -PHP中的数据库连接层


   参考资料:
   1、《PHP设计模式介绍》中的单态模式的实现。
   2、《设计模式解析》中的Template Method模式。
目标
1、实现PHP连接ODBC和MySQL数据库的通用
  访问界面。



2、确保每个访问数据库的PHP页面只有一个对
  所选择数据库的单实例引用。
DbCon类的结构[DbCon.php]


 
Template Method模式部分
do_query()代码:

public function do_query($sql){
          $this->select_db();         //选择数据库
          $dbset = $this->format_select($sql);//查询
          return $dbset;
      }
//do_query()在DbCon抽象类中定义,封装了选择数
   据库和数据库查询操作。select_db()和
   format_select($sql)在子类中实现。
如何实例化数据库连接?
ODBC连接:
$conn = ODBC_Conn::getInstance("agile","","","");
$rs       = $conn->do_query($sql);//执行sql语句,得到返回值
while($item=fetch_row_array($rs)) //遍历数据
{........}

MySQL连接:
$conn=ODBC_Conn::getInstance("localhost:3306","root","","agile");
Singleton 模式部分

模式实现的步骤:
1、为每个数据库连接类添加一个类的私有的静
  态成员变量$instance,引用所需的数据库连
  接实例;
2、为每个数据库连接类添加一个类的公有的静
  态成员函数getInstance,它在$instance为null
  时实例化这个类并设置数据库的连接信息保
  存至对应的对象中。
getInstance()函数代码:
ODBC类:
public static function getInstance($target,$user,$pwd,$db_name) {
   if (!isset(self::$instance)) {
         self::$instance = new ODBC_Conn($target,$user,$pwd
                                              ,$db_name);
         self::$instance->getHandle();
   }
   return self::$instance;
}
// getInstance函数是父类DbCon中定义的抽象方法,所有继承自该
   类的子类都必须实现这个方法。

Contenu connexe

En vedette

Notational systems and abstractions
Notational systems and abstractionsNotational systems and abstractions
Notational systems and abstractionsJeff Long
 
Towards a new metaphysics of complex processes
Towards a new metaphysics of complex processesTowards a new metaphysics of complex processes
Towards a new metaphysics of complex processesJeff Long
 
131217 the recommender revolution : recent data for direct marketing institut...
131217 the recommender revolution : recent data for direct marketing institut...131217 the recommender revolution : recent data for direct marketing institut...
131217 the recommender revolution : recent data for direct marketing institut...Jan Van den Bergh
 
Ik ben een aanrader lezing uba
Ik ben een aanrader lezing ubaIk ben een aanrader lezing uba
Ik ben een aanrader lezing ubaJan Van den Bergh
 

En vedette (8)

Notational systems and abstractions
Notational systems and abstractionsNotational systems and abstractions
Notational systems and abstractions
 
Towards a new metaphysics of complex processes
Towards a new metaphysics of complex processesTowards a new metaphysics of complex processes
Towards a new metaphysics of complex processes
 
131217 the recommender revolution : recent data for direct marketing institut...
131217 the recommender revolution : recent data for direct marketing institut...131217 the recommender revolution : recent data for direct marketing institut...
131217 the recommender revolution : recent data for direct marketing institut...
 
20140522 sanoma mediaparade
20140522 sanoma mediaparade20140522 sanoma mediaparade
20140522 sanoma mediaparade
 
Ekonomika leuven
Ekonomika leuvenEkonomika leuven
Ekonomika leuven
 
Ik ben een aanrader lezing uba
Ik ben een aanrader lezing ubaIk ben een aanrader lezing uba
Ik ben een aanrader lezing uba
 
Voka final 1
Voka final 1Voka final 1
Voka final 1
 
2015 Stima holaba @ Palm
2015 Stima holaba @ Palm 2015 Stima holaba @ Palm
2015 Stima holaba @ Palm
 

Similaire à PHP面向对象实现数据库连接抽象

MongoDB Basics and Tutorial
MongoDB Basics and TutorialMongoDB Basics and Tutorial
MongoDB Basics and TutorialHo Kim
 
深入学习Mongo db
深入学习Mongo db深入学习Mongo db
深入学习Mongo dbLucien Li
 
山頂洞人日記 - 回歸到最純樸的開發
山頂洞人日記 -  回歸到最純樸的開發山頂洞人日記 -  回歸到最純樸的開發
山頂洞人日記 - 回歸到最純樸的開發koji lin
 
基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现yiditushe
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文Guo Albert
 
百度数据库中间层
百度数据库中间层百度数据库中间层
百度数据库中间层yp_fangdong
 
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509tidesq
 
Ibmzf2
Ibmzf2Ibmzf2
Ibmzf2daoopp
 
常用数据库的链接方法
常用数据库的链接方法常用数据库的链接方法
常用数据库的链接方法wensheng wei
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVCjeffz
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xBo-Yi Wu
 
My sql管理基础 李春_v2
My sql管理基础 李春_v2My sql管理基础 李春_v2
My sql管理基础 李春_v2Pickup Li
 
Kid171 chap03 traditional Chinese Version
Kid171 chap03 traditional Chinese VersionKid171 chap03 traditional Chinese Version
Kid171 chap03 traditional Chinese VersionFrank S.C. Tseng
 
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫Justin Lin
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文banq jdon
 
twMVC#10 | ASP.NET MVC Model 的設計與使用
twMVC#10 | ASP.NET MVC Model 的設計與使用twMVC#10 | ASP.NET MVC Model 的設計與使用
twMVC#10 | ASP.NET MVC Model 的設計與使用twMVC
 
ASP.NET MVC Model 的設計與使用 twMVC#10
ASP.NET MVC Model 的設計與使用 twMVC#10ASP.NET MVC Model 的設計與使用 twMVC#10
ASP.NET MVC Model 的設計與使用 twMVC#10twMVC
 
J2ee经典学习笔记
J2ee经典学习笔记J2ee经典学习笔记
J2ee经典学习笔记yiditushe
 
Jdbc中驱动加载的过程分析(下)
Jdbc中驱动加载的过程分析(下)Jdbc中驱动加载的过程分析(下)
Jdbc中驱动加载的过程分析(下)yiditushe
 

Similaire à PHP面向对象实现数据库连接抽象 (20)

MongoDB Basics and Tutorial
MongoDB Basics and TutorialMongoDB Basics and Tutorial
MongoDB Basics and Tutorial
 
深入学习Mongo db
深入学习Mongo db深入学习Mongo db
深入学习Mongo db
 
Metadata4shenzhen Final
Metadata4shenzhen FinalMetadata4shenzhen Final
Metadata4shenzhen Final
 
山頂洞人日記 - 回歸到最純樸的開發
山頂洞人日記 -  回歸到最純樸的開發山頂洞人日記 -  回歸到最純樸的開發
山頂洞人日記 - 回歸到最純樸的開發
 
基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文
 
百度数据库中间层
百度数据库中间层百度数据库中间层
百度数据库中间层
 
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
 
Ibmzf2
Ibmzf2Ibmzf2
Ibmzf2
 
常用数据库的链接方法
常用数据库的链接方法常用数据库的链接方法
常用数据库的链接方法
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVC
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.x
 
My sql管理基础 李春_v2
My sql管理基础 李春_v2My sql管理基础 李春_v2
My sql管理基础 李春_v2
 
Kid171 chap03 traditional Chinese Version
Kid171 chap03 traditional Chinese VersionKid171 chap03 traditional Chinese Version
Kid171 chap03 traditional Chinese Version
 
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文
 
twMVC#10 | ASP.NET MVC Model 的設計與使用
twMVC#10 | ASP.NET MVC Model 的設計與使用twMVC#10 | ASP.NET MVC Model 的設計與使用
twMVC#10 | ASP.NET MVC Model 的設計與使用
 
ASP.NET MVC Model 的設計與使用 twMVC#10
ASP.NET MVC Model 的設計與使用 twMVC#10ASP.NET MVC Model 的設計與使用 twMVC#10
ASP.NET MVC Model 的設計與使用 twMVC#10
 
J2ee经典学习笔记
J2ee经典学习笔记J2ee经典学习笔记
J2ee经典学习笔记
 
Jdbc中驱动加载的过程分析(下)
Jdbc中驱动加载的过程分析(下)Jdbc中驱动加载的过程分析(下)
Jdbc中驱动加载的过程分析(下)
 

PHP面向对象实现数据库连接抽象