首页 > 代码库 > PHP设计模式-适配器模式

PHP设计模式-适配器模式

<?php
//适配器模式
//将不同的函数封装成统一的api
//例如 将mysql pdo mysqli 一致

//接口
interface DataBase
{
function connect($host, $user, $password, $dbname);
}

class Mysql implements DataBase
{
public function connect($host, $user, $password, $dbname)
{
echo __CLASS__.‘ connect‘;
}
}

class MysqlPdo
{
public function connect($host, $user, $password, $dbname)
{
echo __CLASS__.‘ connect‘;
}

}

class MysqlMysqli
{
public function connect($host, $user, $password, $dbname)
{
echo __CLASS__.‘ connect‘;
}
}

$db = new Mysql();
$db->connect();
$db = new MysqlPdo();
$db->connect();
$db = new MysqlMysqli();
$db->connect();

PHP设计模式-适配器模式