我不太確定如何提出這個問題。基本上我試圖讓我的視圖對象是一個從Smarty對象擴展的單例。然後我希望能夠將視圖對象擴展到我的控制器對象。擴展PHP Smarty Singleton類
View對象會將我想要的模板變量分配給我的所有控制器。
我知道我現在有什麼問題,但如果有人能指引我朝着正確的方向發展,那將會很棒。我試圖盡最大努力說出這些。
<?php
defined('SITE_ROOT') ? null : define('SITE_ROOT', $_SERVER['DOCUMENT_ROOT'].'/mvcsandbox');
require_once('Smarty/Smarty.class.php');
class View extends Smarty {
public static $instance=NULL;
public static function getInstance(){
if (self::$instance === null){
self::$instance = new self();
}
return self::$instance;
}
public function __construct() {
$this->template_dir = SITE_ROOT.'/Library/tmp/';
$this->compile_dir = SITE_ROOT.'/Library/tmp/';
$this->config_dir = SITE_ROOT.'/Library/tmp/';
$this->cache_dir = SITE_ROOT.'/Library/tmp/';
$this->assign("var1", "This is from the view class");
}
public static function output() {
self::display('test.tpl');
}
}
class Controller1 extends View {
public function init() {
$this->assign("var2", "This is from the Controller1 class");
}
}
class Controller1_index extends Controller1 {
public function init() {
$this->assign("var3", "This is from the Controller1_index class");
}
}
//$view = View::getInstance();
$controller1 = Controller1::getInstance();
$controller1_index = Controller1_index::getInstance();
$controller1->init();
//$controller1_index->init();
$controller1->output();
?>
我認爲你的意思是「單身人士」:D雖然這個比喻很引人注目。 – Halcyon
是的,「Simpleton」引用是我很好的觸摸,儘管我會誠實地建議你放棄Smarty並嘗試使用[PHP的本機功能](http://codeangel.org/articles/simple-php-template-engine .html)在模板中。 –