【发布时间】:2016-02-22 16:00:23
【问题描述】:
我目前正在创建一个框架,只是为了个人学习项目,即能够在未来总体上评估 php-frameworks。随着我开始对某些框架建立更多的认识,特别是关于他们设计模块并使其在“范围”内可用的方式,在这个项目的开发过程中,我也很困惑为什么几乎所有框架开发者将(视图)模板包含在其类的范围内。
也许有一些开发人员在这些板上漫游,他们能够对此提出建设性意见。
一个例子(为了清楚起见,它被过度简化了)。
为什么要这样做……
<?php
class TemplateInc {
public $template;
public function __construct($page) {
$this->template = $page;
}
public function render() {
require_once $this->template;
}
}
$template_include = new TemplateInc('mypage.html');
$template_include->render();
?>
...什么时候也可以这样做,同时还可以在模板中使用全局范围的变量(如果合适的话)?
<?php
class TemplateName {
public $template;
public function __construct($page) {
$this->template = $page;
}
public function getTemplate() {
return $this->template;
}
}
$templatename = new TemplateName('mypage.html');
require_once $templatename->getTemplate();
?>
【问题讨论】:
标签: php frameworks