ZentaoFramework的代码组织

1,App的公用配置文件

跟module目录平齐的config目录内有config.php和my.php,都可以定义配置项。

在module的controller和model的代码中,可以通过$this->config这种方式访问,这是因为在它们的构造函数中,都有这样的定义:

(出自framework/control.class.php)

global $app, $config, $lang, $dbh;
$this->app    = $app;
$this->config = $config;
$this->lang   = $lang;
$this->dbh    = $dbh;

而这个全局的$config,是在router内被初始化:

(出自framework/route.class.php中loadConfig())

global $config;
if(!is_object($config)) $config = new config();

在配置文件中诸如此类的定义:

$config->views  = ',html,json,';          // Supported view formats.
$config->themes = 'default';  

在初始化完$config后,会通过include关键字被加载进来,实现声明和定义类成员:

(出自framework/route.class.php中loadConfig())

foreach($configFiles as $configFile)
{
            ……
            include $configFile;
            ……

}

$this->config = $config;

从上面可以看出,PHP这种异常灵活的语言接近javascript,假如不遵从一个框架,代码可读性一定会非常差。

2,从PHP到html

每个模块、每个controller中的method都可以创建跟method名字相同的html文件,处理html文件的过程在control类中:

(framework/control.class.php的parseDefault())

extract((array)$this->view);
ob_start();
include $viewFile;
if(isset($hookFiles)) foreach($hookFiles as $hookFile) include $hookFile;
$this->output .= ob_get_contents();
ob_end_clean();

从extract()可以看出,所有跟html相关的PHP变量,都要放在$this->view对象中。

而include关键字实现加载html页面,include关键字的说明是这样的:

当一个文件被包含时,语法解析器在目标文件的开头脱离 PHP 模式并进入 HTML 模式,到文件结尾处恢复。由于此原因,目标文件中应被当作 PHP 代码执行的任何代码都必须被包括在有效的 PHP 起始和结束标记之中。

原文地址:https://www.cnblogs.com/tara/p/2779177.html