- ·上一篇文章:PHP程序员的优化调试技术和技巧
- ·下一篇文章:使用 PHP 5.0创建图形的巧妙方法
理解PHP中的MVC编程之MVC框架简介
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就有了存在的理由,作者会稍后做出讨论它。
require_once('Log.php');
/**
*FR_Object
*
*Thebaseobjectclassformostoftheclassesthatweuseinourframework.
*Providesbasicloggingandset/getfunctionality.
*
*@authorJoeStump<joe@joestump.net>
*@packageFramework
*/
abstractclassFR_Object
{
/**
*$log
*
*@varmixed$logInstanceofPEARLog
*/
protected$log;
/**
*$me
*
*@varmixed$meInstanceofReflectionClass
*/
protected$me;
/**
*__construct
*
*@authorJoeStump<joe@joestump.net>
*@accesspublic
*/
publicfunction__construct()
{
$this->log=Log::factory('file',FR_LOG_FILE);
$this->me=newReflectionClass($this);
}
/**
*setFrom
*
*@authorJoeStump<joe@joestump.net>
*@accesspublic
*@parammixed$dataArrayofvariablestoassigntoinstance
*@returnvoid
*/
publicfunctionsetFrom($data)
{
if(is_array($data)&&count($data)){
$valid=get_class_vars(get_class($this));
foreach($validas$var=>$val){
if(isset($data[$var])){
$this->$var=$data[$var];
}
}
}
}
/**
*toArray
*
*@authorJoeStump<joe@joestump.net>
*@accesspublic
*@returnmixedArrayofmembervariableskeyedbyvariablename
*/
publicfunctiontoArray()
{
$defaults=$this->me->getDefaultProperties();
$return=array();
foreach($defaultsas$var=>$val){
if($this->$varinstanceofFR_Object){
$return[$var]=$this->$var->toArray();
}else{
$return[$var]=$this->$var;
}
}
return$return;
}
/**
*__destruct
*
*@authorJoeStump<joe@joestump.net>
*@accesspublic
*@returnvoid
*/
publicfunction__destruct()
{
if($this->loginstanceofLog){
$this->log->close();
}
}
}
?>
auth.php-这是所有验证功能的底层类。它是从FR_Module里面延伸出来的,主要功能是定义一个基本的验证类如何工作。
跟FR_Module的道理一样,有些类不需要链接到数据库,那么同理,FR_Auth_No就可以被创建应用到不需要验证功能的类上。
abstractclassFR_AuthextendsFR_Module
{
//{{{__construct()
function__construct()
{
parent::__construct();
}
//}}}
//{{{authenticate()
abstractfunctionauthenticate();
//}}}
//{{{__destruct()
function__destruct()
{
parent::__destruct();
}
//}}}
}
?>
module.php-所有模块的心脏
<?php
abstractclassFR_ModuleextendsFR_Object_Web
{
//{{{properties
/**
*$presenter
*
*UsedinFR_Presenter::factory()todeterminewhichpresentation(view)
*classshouldbeusedforthemodule.
*
*@authorJoeStump<joe@joestump.net>
*@varstring$presenter
*@seeFR_Presenter,FR_Presenter_common,FR_Presenter_smarty
*/
public$presenter='smarty';
/**
*$data
*
*Datasetbythemodulethatwilleventuallybepassedtotheview.
*
*@authorJoeStump<joe@joestump.net>
*@varmixed$dataModuledata
*@seeFR_Module::set(),FR_Module::getData()
*/
protected$data=array();
/**
*$name
*
*@authorJoeStump<joe@joestump.net>
*@varstring$nameNameofmoduleclass
*/
public$name;
/**
*$tplFile
*
*@authorJoeStump<joe@joestump.net>
*@varstring$tplFileNameoftemplatefile
*@seeFR_Presenter_smarty
*/
public$tplFile;
/**
*$moduleName
*
*@authorJoeStump<joe@joestump.net>
*@varstring$moduleNameNameofrequestedmodule
*@seeFR_Presenter_smarty
*/
public$moduleName=null;
/**
*$pageTemplateFile
*
*@authorJoeStump<joe@joestump.net>
*@varstring$pageTemplateFileNameofouterpagetemplate
*/
public$pageTemplateFile=null;
//}}}
//{{{__construct()
/**
*__construct
*
*@authorJoeStump<joe@joestump.net>
*/
publicfunction__construct()
{
parent::__construct();
$this->name=$this->me->getName();
$this->tplFile=$this->name.'.tpl';
}
//}}}
//{{{__default()
/**
*__default
*
*Thisfunctionisranbythecontrollerifaneventisnotspecified
*intheuser'srequest.
*
*@authorJoeStump<joe@joestump.net>
*/
abstractpublicfunction__default();
//}}}
//{{{set($var,$val)
/**
*set
*
*Setdataforyourmodule.Thiswilleventuallybepassedtoethe
*presenterclassviaFR_Module::getData().
*
*@authorJoeStump<joe@joestump.net>
*@paramstring$varNameofvariable
*@parammixed$valValueofvariable
*@returnvoid
*@seeFR_Module::getData()
*/
protectedfunctionset($var,$val){
$this->data[$var]=$val;
}
//}}}
//{{{getData()
/**
*getData
*
*Returnsmodule'sdata.
*
*@authorJoeStump<joe@joestump.net>
*@returnmixed
*@seeFR_Presenter_common
*/
publicfunctiongetData()
{
return$this->data;
}
//}}}
//{{{isValid($module)
/**
*isValid
*
*Determinesif$moduleisavalidframeworkmodule.Thisisusedby
*thecontrollertodetermineifthemodulefi
