示例代码:
//From http://www.lai18.com //适配目标,规定的接口将被适配对象实现 interface IDatabase { public function connect($host, $username, $password, $database); public function query($sql); } //适配器 class Mysql implements IDatabase { protected $connect; public function connect($host, $username, $password, $database) { $connect = mysql_connect($host, $username, $password); mysql_select_db($database, $connect); $this->connect = $connect; //... } public function query($sql) { //... } } //适配器 class Postgresql implements IDatabase { protected $connect; public function connect($host, $username, $password, $database) { $this->connect = pg_connect("host=$host dbname=$database user=$username password=$password"); //... } public function query($sql) { //... } } //客户端使用 $client = new Postgresql(); $client->query($sql);
参考:http://www.lai18.com/content/409861.html
原文地址:http://blog.csdn.net/hello_katty/article/details/45664721