首页 > 后端开发 > php教程 > php设计模式 适配器模式,php设计模式_PHP教程

php设计模式 适配器模式,php设计模式_PHP教程

WBOY
发布: 2016-07-12 08:58:23
原创
1024 人浏览过

php设计模式 适配器模式,php设计模式

适配器模式,可以将截然不同的函数接口封装成统一的API;

应用举例,PHP的数据库操作有Mysql、Mysqli、pdo三种,可以用适配器模式统一成一致,类似的场景还有cache适配器,将memcache、redis,file,apc等不同的缓存函数统一成一致。

IDatabase.php

<?<span>php
namespace Baobab;

</span><span>interface</span><span> IDatabase{
    </span><span>function</span> connect(<span>$host</span>, <span>$user</span>, <span>$passwd</span>, <span>$dbname</span><span>);
    </span><span>function</span> query(<span>$sql</span><span>);
    </span><span>function</span> fetch_result(<span>$result</span><span>);
    </span><span>function</span><span> close();
}</span>
登录后复制

Mysqli.php

<?<span>php
namespace Baobab\Database;

</span><span>use</span><span> Baobab\IDatabase;
</span><span>class</span> Mysqli <span>implements</span><span> IDatabase{
    </span><span>protected</span> <span>$conn</span><span>;
    </span><span>function</span> connect(<span>$host</span>, <span>$user</span>, <span>$passwd</span>, <span>$dbname</span><span>){
        </span><span>$conn</span> = <span>mysqli_connect</span>(<span>$host</span>, <span>$user</span>, <span>$passwd</span>, <span>$dbname</span><span>);
        mysqli_set_charset(</span><span>$conn</span>, 'utf8'<span>);
        </span><span>$this</span>->conn = <span>$conn</span><span>;
    }
    
    </span><span>function</span> query(<span>$sql</span><span>) {
        </span><span>return</span> <span>mysqli_query</span>(<span>$this</span>->conn, <span>$sql</span><span>);
    }
    
    </span><span>function</span> fetch_result(<span>$result</span><span>){
        </span><span>return</span> mysqli_fetch_all(<span>$result</span><span>);
    }
    
    </span><span>function</span><span> close() {
        </span><span>mysqli_close</span>(<span>$this</span>-><span>conn);
    }
}</span>
登录后复制

Pdo.php

<?<span>php
namespace Baobab\Database;

</span><span>use</span><span> Baobab\IDatabase;
</span><span>class</span> Pdo <span>implements</span><span> IDatabase{
    </span><span>protected</span> <span>$conn</span><span>;
    </span><span>function</span> connect(<span>$host</span>, <span>$user</span>, <span>$passwd</span>, <span>$dbname</span><span>){
       </span><span>$conn</span> =  <span>new</span> \PDO("mysql:dbname=<span>$dbname</span>;host=<span>$host</span>", <span>$user</span>, <span>$passwd</span><span>);
       </span><span>$this</span>->conn = <span>$conn</span><span>;
    }
    
    </span><span>function</span> query(<span>$sql</span><span>){
        </span><span>return</span> <span>$this</span>->conn->query(<span>$sql</span><span>);
    }
    
    </span><span>function</span> fetch_result(<span>$result</span><span>){
        
    }
    
    </span><span>function</span><span> close() {
        </span><span>unset</span>(<span>$this</span>-><span>conn);
    }
}</span>
登录后复制

index.php

<span>$db</span> = <span>new</span><span> Baobab\Database\Mysqli();
</span><span>$db</span>->connect('127.0.0.1', 'root', '', 'test'<span>);
</span><span>$res</span> = <span>$db</span>->query('select * from ha_cl'<span>);
</span><span>print_r</span>(<span>$db</span>->fetch_result(<span>$res</span><span>));
</span><span>$db</span>->close();
登录后复制

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1103614.htmlTechArticlephp设计模式 适配器模式,php设计模式 适配器模式,可以将截然不同的函数接口封装成统一的API; 应用举例,PHP的数据库操作有Mysql、Mysq...
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板