【问题标题】:Smarty exception: "Please use parent::__construct() to call parent constructor"Smarty 异常:“请使用 parent::__construct() 调用父构造函数”
【发布时间】:2011-06-10 13:08:41
【问题描述】:

我正在学习教程,设置完所有内容后我得到了:

致命错误:未捕获的异常“SmartyException”带有消息“请” 使用 parent::__construct() 调用父构造函数'

这是我的配置文件:

<?php
// SITE_ROOT contains the full path to the tshirtshop folder
define('SITE_ROOT', dirname(dirname(__FILE__)));
// Application directories
define('PRESENTATION_DIR', SITE_ROOT . '/presentation/');
define('BUSINESS_DIR', SITE_ROOT . '/business/');
// Settings needed to configure the Smarty template engine
define('SMARTY_DIR', SITE_ROOT . '/libs/smarty/');
define('TEMPLATE_DIR', PRESENTATION_DIR . 'templates');
define('COMPILE_DIR', PRESENTATION_DIR . 'templates_c');
define('CONFIG_DIR', SITE_ROOT . '/include/configs');
?>

我的目录结构是:

mydomain.com/test/include
mydomain.com/test/libs
mydomain.com/test/presentation

我该如何解决这个错误?

【问题讨论】:

  • 错误信息是不是也告诉你错误在哪一行?
  • 这个PHP4代码是在PHP5环境下运行的吗?
  • 不是 PHP5 环境
  • Bob,不要将问题标题更改为包含“已解决”,而是请接受问题的答案之一。如果他们都没有回答,请添加您自己的答案以及您为解决问题所做的事情,然后接受。谢谢!

标签: php smarty runtime-error


【解决方案1】:

这意味着有一个扩展了 Smarty 类的类,该类使用 __construct 方法,需要包含以下内容:

public function __construct() {
    parent::__construct();
}

这将调用如下所示的 smarty 构造方法:

public function __construct()
{ 
        // selfpointer need by some other class methods
        $this->smarty = $this;
    if (is_callable('mb_internal_encoding')) {
        mb_internal_encoding(SMARTY_RESOURCE_CHAR_SET);
    } 
    $this->start_time = microtime(true); 
    // set default dirs
    $this->template_dir = array('.' . DS . 'templates' . DS);
    $this->compile_dir = '.' . DS . 'templates_c' . DS;
    $this->plugins_dir = array(SMARTY_PLUGINS_DIR);
    $this->cache_dir = '.' . DS . 'cache' . DS;
    $this->config_dir = '.' . DS . 'configs' . DS;
    $this->debug_tpl = SMARTY_DIR . 'debug.tpl';
    if (isset($_SERVER['SCRIPT_NAME'])) {
        $this->assignGlobal('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']);
    } 
} 

【讨论】:

  • 所以我需要在这里改变一些东西:
  • 'template_dir = TEMPLATE_DIR; $this->compile_dir = COMPILE_DIR; $this->config_dir = CONFIG_DIR; } } ?>'
  • 我会尝试在您的项目中搜索“extends Smarty”以尝试找到需要调用 Smarty parent::construct 的类
  • // 调用 Smarty 的构造函数 parent::Smarty();
  • 我不明白什么需要改变?
【解决方案2】:

从您的 cmets 中,您的 Application 类构造函数需要调用 parent::__construct(),而不是 parent::Smarty()

<?php

// Reference Smarty library 
require_once SMARTY_DIR . 'Smarty.class.php';

/* Class that extends Smarty, used to process and display Smarty files */
class Application extends Smarty {

    // Class constructor
    public function __construct() {

        // Call Smarty's constructor 
        // parent::Smarty(); // not this
        parent::__construct(); // this

        // Change the default template directories 
        $this->template_dir = TEMPLATE_DIR;
        $this->compile_dir = COMPILE_DIR;
        $this->config_dir = CONFIG_DIR;
    }

}

?>

【讨论】:

  • 谢谢,但我已经尝试过了 - 我收到一条不同的错误消息:未捕获的异常 'SmartyException' 和消息'无法加载模板文件'store_front.tpl''
  • 也不得不修改配置文件,但它现在可以工作了 - 谢谢
【解决方案3】:

这个link 会很有帮助。 this->Smarty() 构造函数被 PHP4 识别,而 parent::__construct() 被 PHP5 识别。在 PHP5 中使用 this->Smarty() 时发生错误。

【讨论】:

    猜你喜欢
    • 2017-03-07
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 2019-08-30
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多