2011-02-11 147 views
3

我有一個模板類可以解析TPL文件中的數組變量,或者簡單地顯示純HTML文件。解析函數工作正常,但顯示函數返回以下錯誤:調用非對象的成員函數

「致命錯誤:調用C:\ xampp \ htdocs \ clancms \ controllers \ home中的非對象的成員函數display()。第7" 行

PHP這是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; 
      } 

     } 

    } 

} 

?> 
+0

是類模板的文件名是沒有的template.php的template.php – Gaurav 2011-02-11 06:32:45

回答

1

成員變量$模板在您的主頁實例未被初始化。某處需要致電$this->template = new Template();或其他等效物。

這應該可能在Home __construct或父控制器類中。

根據您的控制器INITIALISE功能,我會假設,一個文件不存在對於給定的一個類,所以它與return false;

早退出功能回聲出正在加載的類,如果將它放到數組的末尾,我會感到驚訝。

-4

應該

function index(){ 

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

不,它不應該。 – ShoeLace1291 2011-02-11 06:42:27

相關問題