对非对象调用成员函数

3

我有一个模板类,可以解析TPL文件中的数组变量,也可以简单地显示纯HTML文件。 解析函数正常工作,但显示函数返回以下错误:

"致命错误:在C:\ xampp \ htdocs \ clancms \ controllers \ home.php的第7行上对非对象调用成员函数display()"

这是home.php

class Home extends Controller {

    function index(){

        echo $this->template->display('index_body.tpl');
    }

}
这是模板类。
class Template {

    var $file = '';
    var $vars = '';
    var $themeID = '';
    var $themeTitle = '';
    var $themeDescription = '';
    var $themePath = '';


    function getTheme(){

        if($_SESSION['memberid'] != NULL){

            $query = "
                SELECT memberid, themeid
                FROM members
                WHERE memberID = '".$_SESSION['memberID']."
                LIMIT 1";

            if($query = mysql_query($query)){
                $member = mysql_fetch_assoc($query);


                $query = "
                    SELECT themeID, themeTitle, themeDescription, themePath
                    FROM {DB_PREF} 
                    WHERE themeID = ".$member['themeID']."
                    LIMIT 1";

                if($query = mysql_query($query)){
                    $theme = mysql_fetch_assoc($query);
                    $this->themeID = $theme['themeID'];
                    $this->themePath = BASE_PATH.'/templates/'.$theme['themePath'];
                    $this->themeTitle = $theme['themeTitle'];
                    $this->themeDescription = nl2br(htmlspecialchars($theme['themeDescription']));
                } else {
                    $this->themePath = BASE_PATH.'/templates/default';
                }

            } else {
                $this->themePath = BASE_PATH.'/templates/default';
            }

        } else {
            $this->themePath = BASE_PATH.'/templates/default';
        }

    }

    function parse($file, $vars){

    $this->getTheme();

        if(file_exists($this->themePath.'/'.$file)){
            $file = file_get_contents($this->themePath.'/'.$file);

            foreach($vars as $key => $val){
                $file = str_replace('{'.$key.'}', $val, $file);
            }
            echo $file;
        } else {
            die('Template parser error: the file \''.$this->themePath.'/'.$file.'\' does not exist!');
        }
    }

    function display($file){

        if(file_exists($this->themePath.'/'.$file)){
            $file = file_get_contents($this->themePath.'/'.$file);
            echo $file;
        } else {
            die('Template parser error: the file \''.$this->themePath.'/'.$file.'\' does not exist!');
        }

    }
}

更新

抱歉,我忘记包含那个了。

<?php

class Controller {

    function Controller(){

        $this->initialize();

    }

    function initialize(){

        $classes = array(
                        'load' => 'Load',
                        'uri' => 'URI',
                        'config' => 'Config',
                        'template' => 'Template'
                        );

        foreach($classes as $var => $class){

            if(file_exists($this->app_path.'/classes/'.$class.'.php')){
                require_once(BASE_PATH.'/classes/'.$class.'.php');
                $this->$var =& new $class;
            } else {
                return FALSE;
            }

        }

    }

}

?>

类模板的文件名如果是Template.php而不是template.php。 - Gaurav
2个回答

1

你的Home实例上的成员变量$template没有被初始化。需要在某个地方调用$this->template = new Template();或者等效的代码。

这个应该放在Home __construct函数中或者父Controller类中。

根据你的Controller initialise函数,我猜测给定的类中有一个文件不存在,所以它会提前退出函数并返回false;

输出正在加载的类,并且如果它到达数组末尾,我会感到惊讶。


-4

应该是这样的

function index(){

    echo $this->display('index_body.tpl');
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,