php 调用非对象上的成员函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/254291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:04:50  来源:igfitidea点击:

Call to a member function on a non-object

phpzend-frameworksmarty

提问by Matt

I'm working through Practical Web 2.0 Appicationscurrently and have hit a bit of a roadblock. I'm trying to get PHP, MySQL, Apache, Smarty and the Zend Framework all working correctly so I can begin to build the application. I have gotten the bootstrap file for Zend working, shown here:

我目前正在研究实用 Web 2.0 应用程序,但遇到了一些障碍。我试图让 PHP、MySQL、Apache、Smarty 和 Zend 框架都正常工作,以便我可以开始构建应用程序。我已经获得了 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();
?>

This calls the IndexController. The error comes with the use of this Templater.php to implement Smarty with Zend:

这将调用 IndexController。使用这个 Templater.php 来实现 Smarty with 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()
        { }
    }
?>

The error I am getting when I load the page is this:

我加载页面时遇到的错误是:

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

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

I understand it doesn't see $name as an object, but I don't know how to go about fixing this. Isn't the controller supposed to refer to the index.tpl? I haven't been able to discover what the $name variable represents and how to fix this to get the foundation working.

我知道它没有将 $name 视为对象,但我不知道如何解决这个问题。控制器不应该引用 index.tpl 吗?我一直无法发现 $name 变量代表什么以及如何解决这个问题以使基础工作。

Any help you have is much appreciated!

非常感谢您的任何帮助!

采纳答案by Noah Goodrich

The problem isn't with the $name variable but rather with the $_engine variable. It's currently empty. You need to verify that the path specification to Smarty.class.php is correct.

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

You might try this to begin your debugging:

你可以试试这个来开始你的调试:

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

If it turns out that $_engine is correct at that stage then verify that it is still correctly populated within the render() function.

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

回答by ironkeith

Zend has an example of creating a templating system which implements the Zend_View_Interface here: http://framework.zend.com/manual/en/zend.view.scripts.html#zend.view.scripts.templates.interface

Zend 有一个创建模板系统的示例,该系统在此处实现 Zend_View_Interface:http: //framework.zend.com/manual/en/zend.view.scripts.html#zend.view.scripts.templates.interface

That might save you some time from trying to debug a custom solution.

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

回答by ManjuB

removing the __construct method, from the class, solved the similar issue I was facing.

从类中删除 __construct 方法解决了我面临的类似问题。

回答by nufnuf

Renaming __construct()to Tempater()worked for me.

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