博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
适配器模式(数据库为例)
阅读量:6658 次
发布时间:2019-06-25

本文共 1369 字,大约阅读时间需要 4 分钟。

hot3.png

namespace IMooc;interface IDatebase{    function connect($host,$user,$password,$dbname);    function query($sql);    function close();}
namespace IMooc\Datebase;use IMooc\IDatebase;class MySQL implements IDatebase{    protected $conn;    function connect($host, $user, $password, $dbname)    {        $conn = mysql_connect($host,$user,$password);        mysql_select_db($dbname,$conn);        $this->conn = $conn;    }    function query($sql)    {        return mysql_query($sql,$this->conn);    }    function close()    {        mysql_close($this->conn);    }}
namespace IMooc\Datebase;use IMooc\IDatebase;class MySQLi implements IDatebase{    protected $conn;    function connect($host, $user, $password, $dbname){        $this->conn = mysqli_connect($host, $user, $password, $dbname);    }    function query($sql){        return mysqli_query($this->conn,$sql);    }    function close(){        mysqli_close($this->conn);    }}
namespace IMooc\Datebase;use IMooc\IDatebase;class PDO implements IDatebase{    protected $conn;    function connect($host, $user, $password, $dbname){        $conn = new \PDO("mysql:host=$host;dbname=$dbname",$user,$password);        $this->conn = $conn;    }    function query($sql)    {        return $this->conn->query($sql);    }    function close(){        unset($this->conn);    }}

转载于:https://my.oschina.net/lawlietSady/blog/807391

你可能感兴趣的文章