2013-02-13 61 views
0

我正在尝试与Smarty一起做一些OOP。当我把,例如Smarty中的php函数

$smarty->display('header.tpl'); 

在一个构造函数,一切正常。但是,当我将这段代码放入一个函数并在构造中调用函数时,什么都不会发生。

是否有解决方案,以便我可以在函数中使用代码然后在函数中调用它?

的init.php代码:

class init{ 
    public function __construct(){ 

    $smarty = new Smarty(); 
    $smarty->setTemplateDir('templates'); 
    $smarty->setCompileDir('templates_c'); 
    $smarty->setCacheDir('cache'); 
    $smarty->setConfigDir('configs'); 
    //$smarty->testInstall(); 

    $smarty->display('header.tpl'); 
    $smarty->display('content.tpl'); 
    $smarty->display('footer.tpl'); 

    //----------------------------------- 
    //----------------------------------- 
    //check url 
    //----------------------------------- 
    //----------------------------------- 

    if(isset($_REQUEST['params']) && $_REQUEST['params']!=''){ 
     $aParams = explode('/', $_REQUEST['params']); 
     print_r ($aParams); 
     if(isset($aParams[0])) $this->lang = $aParams[0]; 
     if(isset($aParams[1])) $this->page = $aParams[1]; 
    } 

    if(!$this->lang) $this->lang = 'nl'; 
    if(!$this->page) $this->page = 'home'; 

    //----------------------------------- 
    //----------------------------------- 
    //Functions 
    //----------------------------------- 
    //----------------------------------- 

    $this->buildPage(); 
    $this->buildHeader_Footer(); 

} 

function buildPage(){ 
    require_once('modules/' . $this->page . '/' . $this->page . '.php'); 
    if($this->page == 'home') new home($this->lang, $this->page, $this->action, $this->id, $this->message); 
    else if($this->page == 'contact') new contact($this->lang, $this->page, $this->action, $this->id, $this->message); 

} 

function buildHeader_Footer(){ 
    $smarty->display('header.tpl'); 
    $smarty->display('footer.tpl'); 
} 

} 

的index.php代码:

require('smarty/libs/Smarty.class.php'); 

require_once ('modules/init/init.php'); 
$init = new init(); 

回答

0

更新(问题已经改变后)

看来,你想到的是,$smarty变量访问从构造函数中创建后的所有类方法。这是错误的。类变量可以在类中使用$this访问。所以你必须写:

$this->smarty-> ... 

每当使用它。


我不能说您的解决方案的问题到底是什么,因为发布的代码是不完整的。但是你想做的事情应该工作。

作为一个例子,我会有这样一类:

class SimpleView { 

    /** 
    * Note that smarty is an instance var. This means that var 
    * is only available via `$this` in the class scope 
    */ 
    protected $smarty; 


    /** 
    * Constructor will be called if write 'new SimpleView()' 
    */ 
    public function __construct(){ 
     // note $this 
     $this->smarty = new Smarty(); 
     $this->smarty->setTemplateDir('templates'); 
     $this->smarty->setCompileDir('templates_c'); 
     $this->smarty->setCacheDir('cache'); 
     $this->smarty->setConfigDir('configs'); 
    } 


    /** 
    * The build function is public. It can be called from 
    * outside of the class 
    */ 
    public function build(){ 
     $this->smarty->display('header.tpl'); 
     $this->smarty->display('content.tpl'); 
     $this->smarty->display('footer.tpl'); 
    } 
} 

,并使用它是这样的:

$view = new SimpleView(); 
$view->build(); 
+0

我编辑的代码。我试图按照您的建议进行更改,但似乎没有奏效。 – Axll 2013-02-13 18:38:59

+0

我在代码中看不到'class'构造。另外,正如我所说:** note **'$ this'。 – hek2mgl 2013-02-13 18:40:00

+0

忘记复制最后一次编辑。我会尝试添加$ this – Axll 2013-02-13 18:46:09