调用非对象上的成员函数

时间:2008-10-31 17:25:45

标签: php zend-framework smarty

我目前正在处理Practical Web 2.0 Appications并且遇到了一些障碍。我正在尝试让PHP,MySQL,Apache,Smarty和Zend Framework都正常工作,这样我就可以开始构建应用程序了。我已经获得了Zend工作的bootstrap文件,如下所示:

<?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变量代表什么以及如何解决这个问题以使基础工作。

非常感谢您的任何帮助!

4 个答案:

答案 0 :(得分:5)

问题不在于$ name变量,而在于$ _engine变量。它目前是空的。您需要验证Smarty.class.php的路径规范是否正确。

您可以尝试这样开始调试:

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

如果事实证明$ _engine在该阶段是正确的,那么确认它仍然在render()函数中正确填充。

答案 1 :(得分:0)

Zend有一个创建模板系统的例子,它在这里实现了Zend_View_Interface:http://framework.zend.com/manual/en/zend.view.scripts.html#zend.view.scripts.templates.interface

这可能会为您节省一些时间来尝试调试自定义解决方案。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

__construct()重命名为Tempater()为我工作。

相关问题