gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:23:44 24 4
gpt4 key购买 nike

我正在处理 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。使用此 Templeter.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 :在第 60 行调用/var/www/phpweb20/include/Templater.php 中非对象的成员函数 fetch()

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

非常感谢您提供的任何帮助!

最佳答案

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

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

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

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

关于php - 调用非对象的成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/254291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com