2013-02-13 2 views
0

Я пытаюсь сделать ООП вместе с Smarty. Когда я положил, например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

Update (после того, как вопрос изменился)

кажется, что вы ожидаете, что переменная $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

+0

Я не вижу конструкцию 'class' в вашем коде. Кроме того, как я сказал: ** обратите внимание ** на '$ this'. – hek2mgl

+0

Забыл скопировать это с последним правлением. Я попытаюсь добавить $ this – Axll