2008-10-31 23 views
6

我目前正在通過Practical Web 2.0 Appications工作,並且遇到了一些障礙。我試圖讓PHP,MySQL,Apache,Smarty和Zend Framework都正常工作,所以我可以開始構建應用程序。我已經獲得了Zend工作的引導文件,如下所示:調用非對象上的成員函數

<?php 
    require_once('Zend/Loader.php'); 
    Zend_Loader::registerAutoload(); 

    // load the application configuration 
    $config = new Zend_Config_Ini('../settings.ini', 'development'); 
    Zend_Registry::set('config', $config); 


    // create the application logger 
    $logger = new Zend_Log(new Zend_Log_Writer_Stream($config->logging->file)); 
    Zend_Registry::set('logger', $logger); 


    // connect to the database 
    $params = array('host'  => $config->database->hostname, 
        'username' => $config->database->username, 
        'password' => $config->database->password, 
        'dbname' => $config->database->database); 

    $db = Zend_Db::factory($config->database->type, $params); 
    Zend_Registry::set('db', $db); 


    // handle the user request 
    $controller = Zend_Controller_Front::getInstance(); 
    $controller->setControllerDirectory($config->paths->base . 
             '/include/Controllers'); 

    // setup the view renderer 
    $vr = new Zend_Controller_Action_Helper_ViewRenderer(); 
    $vr->setView(new Templater()); 
    $vr->setViewSuffix('tpl'); 
    Zend_Controller_Action_HelperBroker::addHelper($vr); 

    $controller->dispatch(); 
?> 

這調用了IndexController。錯誤帶有使用這種Templater.php的與Zend實現的Smarty:

<?php 
    class Templater extends Zend_View_Abstract 
    { 
     protected $_path; 
     protected $_engine; 

     public function __construct() 
     { 
      $config = Zend_Registry::get('config'); 

      require_once('Smarty/Smarty.class.php'); 

      $this->_engine = new Smarty(); 
      $this->_engine->template_dir = $config->paths->templates; 
      $this->_engine->compile_dir = sprintf('%s/tmp/templates_c', 
                $config->paths->data); 

      $this->_engine->plugins_dir = array($config->paths->base . 
               '/include/Templater/plugins', 
               'plugins'); 
     } 

     public function getEngine() 
     { 
      return $this->_engine; 
     } 

     public function __set($key, $val) 
     { 
      $this->_engine->assign($key, $val); 
     } 

     public function __get($key) 
     { 
      return $this->_engine->get_template_vars($key); 
     } 

     public function __isset($key) 
     { 
      return $this->_engine->get_template_vars($key) !== null; 
     } 

     public function __unset($key) 
     { 
      $this->_engine->clear_assign($key); 
     } 

     public function assign($spec, $value = null) 
     { 
      if (is_array($spec)) { 
       $this->_engine->assign($spec); 
       return; 
      } 

      $this->_engine->assign($spec, $value); 
     } 

     public function clearVars() 
     { 
      $this->_engine->clear_all_assign(); 
     } 

     public function render($name) 
     { 
      return $this->_engine->fetch(strtolower($name)); 
     } 

     public function _run() 
     { } 
    } 
?> 

當網頁加載完畢後,我得到的錯誤是這樣的:

Fatal error: Call to a member function fetch() on a non-object in /var/www/phpweb20/include/Templater.php on line 60

我的理解沒有關係沒有看到$ name作爲對象,但我不知道如何去解決這個問題。控制器是否應該引用index.tpl?我一直無法發現$ name變量代表什麼,以及如何解決這個問題以使基礎工作。

任何幫助你都非常感謝!

+0

實際上,這個錯誤並不是$ name是不明確的,而是$ this - > _引擎是不明確的 - 這就是解析器看不到的對象。 – 2008-10-31 17:50:05

回答

5

問題不在$ name變量中,而是在$ _engine變量中。它目前是空的。您需要驗證Smarty.class.php的路徑規範是否正確。如果事實證明,$ _engine是在那個階段正確的,那麼驗證它是否仍然正確填充渲染()函數中

$this->_engine = new Smarty(); 
print_r($this->_engine); 

你可以試試這個,開始你的調試。

0

從類中刪除__construct方法解決了我面臨的類似問題。

0

更名__construct()Tempater()爲我工作。

相關問題