0

Smarty と一緒に OOP を実行しようとしています。たとえば、

$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();
4

1 に答える 1

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();
于 2013-02-13T18:22:12.710 に答える