PHP design mode Adapter mode

Source: Internet
Author: User
Tags php database

1. Description

1. Adapter Mode: 可以将截然不同的函数接口封装成统一的API
2. Practical application example, PHP database operation has MySQL, mysqli, Pdo3 species, can be unified into the same adapter mode. A similar scenario is a cache adapter that unifies different cache functions, such as MEMCACHE,REDIS,FILE,APC, into a consistent
3. The following example can help you deal with various database link functions, MySQL mysqli, PDO random selection, operation is abstracted into a unified interface client does not need to modify a row of database link mode

2. Create an interface
interface IDatabase{    function connect($host, $user, $passwd, $dbname);    function query($sql);    function close();}
3.Mysql Package
 class MySQL implements idatabase{    protected $conn; function connect($host, $user, $passwd, $dbname )    {        $conn= Mysql_connect ($host,$user,$passwd); mysql_select_db ($dbname,$conn);$this->conn =$conn; } function query($sql) {        $res= mysql_query ($sql,$this->conn);return $res; } function close() {Mysql_close ($this->conn); }}
4.MYSQLI Package
 class mysqli implements idatabase{    protected $conn; function connect($host, $user, $passwd, $ dbname) {        $conn= Mysqli_connect ($host,$user,$passwd,$dbname);$this->conn =$conn; } function query($sql) {        returnMysqli_query ($this->conn,$sql); } function close() {Mysqli_close ($this->conn); }}
5.PDO Package
 class PDO implements idatabase{    protected $conn; function connect($host, $user, $passwd, $ dbname) {        $conn=New\pdo ("mysql:host= $host;d bname= $dbname",$user,$passwd);$this->conn =$conn; } function query($sql) {        return $this->conn->query ($sql); } function close() {        unset($this->conn); }}
6. Instance invocation, (Unified interface Invocation)
$db=NewPDO ();$db->connect (' 127.0.0.1 ',' Root ',' Root ',' Test ');$db->query (' show databases ');$db->close ();$db= Mysqli ();$db->connect (' 127.0.0.1 ',' Root ',' Root ',' Test ');$db->query (' show databases ');$db->close ();$db= MySQl PDO ();$db->connect (' 127.0.0.1 ',' Root ',' Root ',' Test ');$db->query (' show databases ');$db->close ();

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

PHP design mode Adapter mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.