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

发布于 2024-11-14 18:29:11 字数 855 浏览 1 评论 0原文

我正在遵循教程,在设置完所有内容后我得到:

致命错误:未捕获异常“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

如何修复此错误?

I'm following a tutorial and after setting everything up I'm getting:

Fatal error: Uncaught exception 'SmartyException' with message 'Please
use parent::__construct() to call parent constuctor'

This is my config file:

<?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');
?>

My directory structure is:

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

How can I fix this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我很坚强 2024-11-21 18:29:11

这意味着有一个类扩展了 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']);
    } 
} 

This means there is a class extending the Smarty class that is using __construct method that needs to contain something like:

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

this will call the smarty construct method which looks something like this:

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']);
    } 
} 
暖阳 2024-11-21 18:29:11

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

}

?>

From your comments, your Application class constructor needs to call parent::__construct(), not 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;
    }

}

?>
我们的影子 2024-11-21 18:29:11

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

this link will be helpful.
this->Smarty() constructor is recognized by PHP4 and parent::__construct() by PHP5. Error occurs when you use this->Smarty() in PHP5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文