Smarty异常:“请使用parent :: __ construct()来调用父构造函数”

时间:2011-06-10 13:08:42

标签: php smarty runtime-error

我正在关注一个教程,在设置好所有内容后,我得到了:

  

致命错误:未捕获的异常'SmartyException',消息'Please   使用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

如何解决此错误?

3 个答案:

答案 0 :(得分:7)

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

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']);
    } 
} 

答案 1 :(得分:3)

根据您的评论,您的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;
    }

}

?>

答案 2 :(得分:1)

link会有所帮助。 this-&gt; Smarty()构造函数由PHP4识别,而parent :: __ construct()由PHP5识别。在PHP5中使用this-&gt; Smarty()时出错。