【建立底层】
我是一个PEAR尤其是PEAR_Error类的爱好者。PHP5引入了一个新的内建类“Exception”取代了PEAR_Error。但是PEAR_Error拥有一些比Exception还要实用的特性。所以,在此系列文章中的MVC框架实例将用到它来做错误处理。无论如何,我还是要用到Exception获得从构造器中的错误,因为它们本身不能传回错误。
设计这些基础类的目的有如下几点:
利用PEAR快速添加功能到基础类
建立小巧、可反复实用的抽象类以便让使用者在此框架中快速开发出应用程序
用phpDocumentor给所有的基础类生成文档
类的层次看起来会像这样:
-FR_Object将会提供基础的功能以供其他所有对象使用(包括logging,一般的setFrom(),toArray())
-FR_Object_DB是一个小层面,给子类提供数据库链接等功能
-FR_Module是所有应用(又称模块、模型等等)的底层类
-FR_Auth是所有验证机制的底层类
·FR_Auth_User是一个验证类,用来验证所有需要验证用户是否登陆的模块
·FR_Auth_No是所有不需要验证的模块的“假验证类”
-FR_Presenter是所有用来处理载入和显示应用的底层类
-FR_Presenter_Smarty是包含了载入不同驱动器能力的显示层。Smarty是一个非常好的模板类,它拥有内建的缓存机制以及一个活跃的开发团体(译者注:这分明就是打广告嘛~)
·FR_Presenter_debug是调试部分的显示层。依靠它,开发者能够调试应用程序并给他们除错
·FR_Presenter_rest是一个可以让开发者能够以XML方式输出应用程序的REST显示层
从以上的基础类结构上,你应该可以看到这个MVC框架的不同部分。FR_Module提供所有模块所需要的东西,而FR_Presenter则提供不同的显示方法。在此系列文章中的下一篇中,我将创建控制器将这上面所有的基础类结合在一块。
【代码标准】
在你正式编写代码之前,应该坐下来跟你的合伙人(或者你自己)好好讨论(或思考)一下代码标准。MVC编程的整体思想围绕着两点:代码的可再利用性(减少偶合)和代码的标准化。我推荐至少应该考虑到如下几点:
首先要考虑的是变量命名和缩写标准。不要因为这个跟你的合作伙伴大吵一通,但是一旦定下来的标准,就要自始至终地遵从,尤其是写底层代码(基础类)的时候。
定制一个标准前缀,用在所有的函数、类和全局变量上。不走运的是,PHP不支持“namespace(命名空间)”。所以要想避免混淆变量名和发生的冲突,用一个前缀是个明智的做法。我在整篇文章中将使用“FR_”作为这样的前缀。
【编写底层】
文件层次规划很重要。基本的层次规划很简单且在一定程度上是严格定义的:
/ config.php index.php includes/ Auth.php Auth/ No.php User.php Module.php Object.php Object/ DB.php Presenter.php Presenter/ common.php debug.php smarty.php Smarty/ modules/ example/ config.php example.php tpl/ example.tpl tpl/ default/ cache/ config/ templates/ templates_c/ |
你可能会想这样的文件层次肯定代表了很多的代码!没错,但是你能够完成它的。在整个系列结束后,你会发现你的编程将会变得更简单并且开发速度会得到很大的提升。
在文件层次里面,所有的基础类都在includes文件夹内。每一个功能模块,都用一个配置文件,至少一个模块文件和一个模板文件。所有的模块包含在modules文件夹内。我已经习惯了将模板文件放在单独的外部文件夹内,也就是tpl文件夹。
config.php-中枢配置文件,包含所有的全局配置变量。
index.php-控制器,在接下来的一篇文章中会详细叙述。
object.php-所有基础类的底层类,提供绝大部分类需要的功能。FR_Object_DB继承这个类并提供数据库链接。
结构的基本概念就是,让所有的子类都继承一个中枢类以便它们都共享一些共同的特性。你完全可以把链接数据库的功能放进FR_Object,但是并不是所有类都需要这个功能的,所以FR_Object_DB就有了存在的理由,作者会稍后做出讨论它。
<?php require_once('Log.php');
/** * FR_Object * * The base object class for most of the classes that we use in our framework. * Provides basic logging and set/get functionality. * * @author Joe Stump <joe@joestump.net> * @package Framework */
abstract class FR_Object { /** * $log * * @var mixed $log Instance of PEAR Log */
protected $log; /** * $me * * @var mixed $me Instance of ReflectionClass */
protected $me; /** * __construct * * @author Joe Stump <joe@joestump.net> * @access public */
public function __construct() { $this->log = Log::factory('file',FR_LOG_FILE); $this->me = new ReflectionClass($this); }
/** * setFrom * * @author Joe Stump <joe@joestump.net> * @access public * @param mixed $data Array of variables to assign to instance * @return void */
public function setFrom($data) { if (is_array($data) && count($data)) { $valid = get_class_vars(get_class($this)); foreach ($valid as $var => $val) { if (isset($data[$var])) { $this->$var = $data[$var]; } } } }
/** * toArray * * @author Joe Stump <joe@joestump.net> * @access public * @return mixed Array of member variables keyed by variable name */
public function toArray() { $defaults = $this->me->getDefaultProperties(); $return = array(); foreach ($defaults as $var => $val) { if ($this->$var instanceof FR_Object) { $return[$var] = $this->$var->toArray(); } else { $return[$var] = $this->$var; } }
return $return; }
/** * __destruct * * @author Joe Stump <joe@joestump.net> * @access public * @return void */
public function __destruct() { if ($this->log instanceof Log) { $this->log->close(); } } }
?> |
auth.php-这是所有验证功能的底层类。它是从FR_Module里面延伸出来的,主要功能是定义一个基本的验证类如何工作。
跟FR_Module的道理一样,有些类不需要链接到数据库,那么同理,FR_Auth_No就可以被创建应用到不需要验证功能的类上。
<?php abstract class FR_Auth extends FR_Module { // {{{ __construct() function __construct() { parent::__construct(); } // }}} // {{{ authenticate() abstract function authenticate(); // }}}
// {{{ __destruct()
function __destruct() { parent::__destruct(); } // }}} }
?>
module.php-所有模块的心脏
<?php abstract class FR_Module extends FR_Object_Web { // {{{ properties /** * $presenter * * Used in FR_Presenter::factory() to determine which presentation (view) * class should be used for the module. * * @author Joe Stump <joe@joestump.net> * @var string $presenter * @see FR_Presenter, FR_Presenter_common, FR_Presenter_smarty */ public $presenter = 'smarty'; /** * $data * * Data set by the module that will eventually be passed to the view. * * @author Joe Stump <joe@joestump.net> * @var mixed $data Module data * @see FR_Module::set(), FR_Module::getData() */
protected $data = array();
/** * $name * * @author Joe Stump <joe@joestump.net> * @var string $name Name of module class */
public $name;
/** * $tplFile * * @author Joe Stump <joe@joestump.net> * @var string $tplFile Name of template file * @see FR_Presenter_smarty */
public $tplFile;
/** * $moduleName * * @author Joe Stump <joe@joestump.net> * @var string $moduleName Name of requested module * @see FR_Presenter_smarty */
public $moduleName = null; /** * $pageTemplateFile * * @author Joe Stump <joe@joestump.net> * @var string $pageTemplateFile Name of outer page template */
public $pageTemplateFile = null; // }}}
// {{{ __construct() /** * __construct * * @author Joe Stump <joe@joestump.net> */
public function __construct() { parent::__construct(); $this->name = $this->me->getName(); $this->tplFile = $this->name.'.tpl'; }
// }}} // {{{ __default()
/** * __default * * This function is ran by the controller if an event is not specified * in the user's request. * * @author Joe Stump <joe@joestump.net> */
abstract public function __default(); // }}} // {{{ set($var,$val)
/** * set * * Set data for your module. This will eventually be passed toe the * presenter class via FR_Module::getData(). * * @author Joe Stump <joe@joestump.net> * @param string $var Name of variable * @param mixed $val Value of variable * @return void * @see FR_Module::getData() */
protected function set($var,$val) { $this->data[$var] = $val; } // }}} // {{{ getData()
/** * getData * * Returns module's data. * * @author Joe Stump <joe@joestump.net> * @return mixed * @see FR_Presenter_common */
public function getData() { return $this->data; } // }}} // {{{ isValid($module)
/** * isValid * * Determines if $module is a valid framework module. This is used by * the controller to determine if the module fits into our framework's * mold. If it extends from both FR_Module and FR_Auth then it should be * good to run. * * @author Joe Stump <joe@joestump.net> * @static * @param mixed $module * @return bool */
public static function isValid($module) { return (is_object($module) && $module instanceof FR_Module && $module instanceof FR_Auth); } // }}} // {{{ __destruct()
public function __destruct() { parent::__destruct(); } // }}} } ?>
presenter.php-表述层的核心。
<?php class FR_Presenter { // {{{ factory($type,FR_Module $module) /** * factory * * @author Joe Stump <joe@joestump.net> * @access public * @param string $type Presentation type (our view) * @param mixed $module Our module, which the presenter will display * @return mixed PEAR_Error on failure or a valid presenter * @static */
static public function factory($type,FR_Module $module) { $file = FR_BASE_PATH.'/includes/Presenter/'.$type.'.php'; if (include($file)) { $class = 'FR_Presenter_'.$type; if (class_exists($class)) { $presenter = new $class($module); if ($presenter instanceof FR_Presenter_common) { return $presenter; } return PEAR::raiseError('Invalid presentation class: '.$type); } return PEAR::raiseError('Presentation class not found: '.$type); } return PEAR::raiseError('Presenter file not found: '.$type); } // }}} }
?> |
下一篇里,我将介绍控制器(MVC中的Controller,本文的index.php)的构造。第三篇里,我将介绍表述层(MVC里面的View)。第四篇里,我将用具体模块为例建立一个应用(MVC里面的Module或Model)。 
说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
2/2 首页 上一页 1 2 |