php设计模式及在框架设计中的经典应用

上传人:aa****6 文档编号:54133820 上传时间:2018-09-08 格式:PPT 页数:32 大小:4.35MB
返回 下载 相关 举报
php设计模式及在框架设计中的经典应用_第1页
第1页 / 共32页
php设计模式及在框架设计中的经典应用_第2页
第2页 / 共32页
php设计模式及在框架设计中的经典应用_第3页
第3页 / 共32页
php设计模式及在框架设计中的经典应用_第4页
第4页 / 共32页
php设计模式及在框架设计中的经典应用_第5页
第5页 / 共32页
点击查看更多>>
资源描述

《php设计模式及在框架设计中的经典应用》由会员分享,可在线阅读,更多相关《php设计模式及在框架设计中的经典应用(32页珍藏版)》请在金锄头文库上搜索。

1、PHP设计模式及在框架设计中的经典应用,目标,了解设计模式与框架 给出一个可行的学习设计模式的方法 介绍几种常见的设计模式,Agenda,设计模式简述 框架简述 设计模式与框架 软件开发演进过程 框架要解决的问题 Factory Singleton Register Adapter Proxy Active Record MVC,什么是模式?,demo for(int i=0;i_data$key = $value;public function get($key) return isset($this-_data$key) ? $this-_data$key : null; 使用 $ctx

2、= Reistry:getInstance(); $ctx-set(xxxx,oooo) $value = $ctx-get(xxxx“) 使用单实例的全局对象来代替全局的变量,模式三:Registry + Factory(续),保存读写分离的两个mysql连接 保存 $ctx = Registry:getInstance(); $ctx-set(db_read)=Db_Factory:getInstance(db_read) $ctx -set(db_write)=Db_Factory:getInstance(db_read)使用 $dbReadObj = $ctx-get(db_read)

3、 $dbWriteObj = $ctx-get(db_write),场景四,需要同时支持mysql和postgresql连接 实现 针对每个数据库,单独写一个驱动实例 Db_Factory:getMysqlDB() Db_Factory:getPostgresqlDB() 问题 如何保持不同DB接口的一致性?,模式四:Adapter Pattern,定义接口 interface IDB public function connect();public function error();public function errno();public static function escape_s

4、tring($string);public function query($query);public function fetchArray($result);public function fetchRow($result);public function fetchAssoc($result);public function fetchObject($result);public function numRows($result);public function close();/接口定义供示例用 ,模式四:Adapter Pattern(续),针对不同的DB实现 Driver_DB_M

5、ysql implements IDB private $link; public function connect($server=, $username=, $password=, $new_link=true, $client_flags=0)$this-link = mysql_connect($server, $username, $password, $new_link, $client_flags); public function errno()return mysql_errno($this-link); / ,模式四:Adapter Pattern(续),Adpater+F

6、actory 实现 DB_Factory public static function getInstance($adapter=Mysql) /参数化工厂if(!isset(self:$_db$adapter) | ($_db$adapter instanceof IDB) if (include_once Drivers/DB/ . $type . .php) $classname = Driver_DB_ .ucfirst( $type);self:$_db$adapter = new $classname; else throw new Exception(Driver not fou

7、nd);return self:$_db$adapter; 使用 DB_Factory:getInstance(mysql),场景五,需要延迟初始化一些资源 class UserController private $db; /构造函数中初始化资源 function _construct() $this-db = DB_Factory:getInstance(mysql) public function manages() $sql = “select * from managers“; $users = $tthis-db-getResults($sql); public function

8、foo() echo “haha“ 问题 foo()方法不需要DB连接,在构造函数里面去连接数据库是不是额外消耗连接资源?,模式五:Proxy,代理模式v1 实现共同的接口 class Proxy_DB_Mysql implements IDB private $db;public function query($query) if(!$this-db) $this-db= DB_Factory:getInstance(mysql) return $this-db-query($query); 延迟初始化、把耗费资源的操作延迟到必须的时候 问题 每个方法都需重写一次?,Proxy,代理模式v2

9、 使用magic方法 class Proxy_DB_Mysql private $mysql;public function _call($method,$args) if(!$this-mysql) $this-mysql = DB_Factory:getInstance(mysql) retrun call_user_func_array(array($this-mysql,$method),$args); 使用:function foo() $this-proxy-query($sql);v1 和v2这两种方式哪一个更好呢?,场景六,每次我们都需要原生的sql ? function foo() $sql = “select * from users“; 我希望OO,不要有那么多SQL 我不懂SQL how to ? nosql:),模式六:Active Record,使用示例 $User = new User(); $User-name = hanyh; $User-address = 北京; $User-save(); or $data = array(name=hanyh,address=beijing); $User-save($data);,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > PPT模板库 > 教育/培训/课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号