开发者

Call to a member function on a non-object

I have a template class that can either parse array variables in TPL files, or simple display a pure HTML file. The parse function works fine, but the display function returns the following error:

"Fatal error: Call to a member function display() on a non-object in C:\xampp\htdocs\clancms\controllers\home.php on line 7"

This is home.php

class Home extends Controller {

    function index(){

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

}

This is the template class

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!');
        }

    }
}

Update

Sorry, I forgot to include that

<?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;
            }

        }

    }

}

?>


The member variable $template on your Home instance is not being initialized. Somewhere there needs to be a call to $this->template = new Template(); or something equivalent.

This should probably be in the Home __construct or in the parent Controller class.

Based on your Controller initialise function, I would assume that a file does not exist for one of the given classes and so it is exiting the function early with return false;

Echo out the classes that are being loaded, and I would be surprised if its making it to the end of the array.


It should be

function index(){

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜