调用一个非对象成员函数

6

我目前正在学习实用的Web 2.0应用,但遇到了一些困难。我正在尝试正确地安装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将Smarty与Zend实现时会出现错误:
<?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()
        { }
    }
?>
我在加载页面时遇到的错误是这样的: 致命错误:在 /var/www/phpweb20/include/Templater.php 的第60行,对非对象调用成员函数fetch()。 我知道它没有将$name视为对象,但我不知道怎样解决这个问题。控制器不应该引用index.tpl吗?我一直没有找到$name变量代表什么以及如何修复它以使基础工作起来。 非常感谢您所提供的任何帮助!

实际上,这个 bug 不是 $name 不明确,而是 $this->_engine 不明确 - 这就是解析器无法识别为对象的原因。 - Peter Bailey
4个回答

5
问题不在于$name变量,而是在于$_engine变量。它目前为空。您需要验证Smarty.class.php的路径规范是否正确。 您可以尝试以下方法开始调试:
$this->_engine = new Smarty();
print_r($this->_engine);
如果在那个阶段$_engine是正确的,那么请验证它是否在render()函数中仍然正确地填充。

0
从类中删除__construct方法解决了我遇到的类似问题。

0

0

__construct() 重命名为 Tempater() 对我有用。


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