Php 是否可以在派生构造函数中设置smarty目录?

Php 是否可以在派生构造函数中设置smarty目录?,php,smarty,config,derived-class,Php,Smarty,Config,Derived Class,嘿,我正试着这样做: <?php class MySmarty extends Smarty { public function __construct() { parent::__construct(); $smartyRoot = '/home/smarty/'; parent::setTemplateDir($smartyRoot. "/templates/"); parent::setCompileDir(

嘿,我正试着这样做:

<?php
class MySmarty extends Smarty {
    public function __construct() {
        parent::__construct();

        $smartyRoot = '/home/smarty/';

        parent::setTemplateDir($smartyRoot. "/templates/");
        parent::setCompileDir($smartyRoot."/templates_c/");
        parent::setCacheDir($smartyRoot."/cache/");
        parent::setConfigDir($smartyRoot."/configs/");
    }
}

$smarty = new MySmarty();
$smarty->display("index.tpl");
?>
class MySmarty extends Smarty {

 function __construct() {
    global $path, $template_dir, $production;


    parent::__construct(); 

    $this->template_dir = $template_dir;
    $this->compile_dir = "$template_dir/tpl_c";
    $this->config_dir = "$template_dir/configs";
    $this->cache_dir = "$template_dir/cache";
(...)

但由于出现
SmartyException(“无法加载模板文件”)
而失败。从
smarty\u internal\u template.php
第163行,在进行任何显示之前,它会检查
$this
是否存在

我的系统似乎设置正确,因为建议的方法(调用
$smarty->set*Dir($smartyRoot.foo');
有效


有什么想法吗?

您甚至可以在构造函数的上下文中使用
$this
。因此,请尝试:

    $this->setTemplateDir($smartyRoot. "/templates/");
    $this->setCompileDir($smartyRoot."/templates_c/");
    $this->setCacheDir($smartyRoot."/cache/");
    $this->setConfigDir($smartyRoot."/configs/");

应该可以使用。

即使在构造函数的上下文中,也可以使用
$this
。因此请尝试:

    $this->setTemplateDir($smartyRoot. "/templates/");
    $this->setCompileDir($smartyRoot."/templates_c/");
    $this->setCacheDir($smartyRoot."/cache/");
    $this->setConfigDir($smartyRoot."/configs/");

应该可以工作。

你确定问题出在代码上吗?我在所有应用程序中都使用这样的代码:

<?php
class MySmarty extends Smarty {
    public function __construct() {
        parent::__construct();

        $smartyRoot = '/home/smarty/';

        parent::setTemplateDir($smartyRoot. "/templates/");
        parent::setCompileDir($smartyRoot."/templates_c/");
        parent::setCacheDir($smartyRoot."/cache/");
        parent::setConfigDir($smartyRoot."/configs/");
    }
}

$smarty = new MySmarty();
$smarty->display("index.tpl");
?>
class MySmarty extends Smarty {

 function __construct() {
    global $path, $template_dir, $production;


    parent::__construct(); 

    $this->template_dir = $template_dir;
    $this->compile_dir = "$template_dir/tpl_c";
    $this->config_dir = "$template_dir/configs";
    $this->cache_dir = "$template_dir/cache";
(...)

你确定问题出在代码上吗?我在所有应用程序中都使用如下代码:

<?php
class MySmarty extends Smarty {
    public function __construct() {
        parent::__construct();

        $smartyRoot = '/home/smarty/';

        parent::setTemplateDir($smartyRoot. "/templates/");
        parent::setCompileDir($smartyRoot."/templates_c/");
        parent::setCacheDir($smartyRoot."/cache/");
        parent::setConfigDir($smartyRoot."/configs/");
    }
}

$smarty = new MySmarty();
$smarty->display("index.tpl");
?>
class MySmarty extends Smarty {

 function __construct() {
    global $path, $template_dir, $production;


    parent::__construct(); 

    $this->template_dir = $template_dir;
    $this->compile_dir = "$template_dir/tpl_c";
    $this->config_dir = "$template_dir/configs";
    $this->cache_dir = "$template_dir/cache";
(...)

模板目录可以是数组,它可以在内部执行此操作。还有addTemplateDir()。Smarty将按顺序遍历它们。使用$this->是构造函数的正确方法。

模板目录可以是数组,它可以在内部执行此操作。还有addTemplateDir().Smarty将按顺序遍历它们。使用$this->是构造函数的正确方法。

嗯。不,我不完全确定,我只是认为我已经排除了所有其他选项。执行您实际执行的操作(即
$this->*\u dir='foo';
而不是
$this->setFooDir()
这有点奇怪。嗯。不,我不完全确定,我只是认为我已经排除了所有其他选择。做你所做的实际工作(即
$this->*\u dir='foo';
而不是
$this->setFooDir()这有点奇怪。实际上我就是这样开始的,但它完全不起作用。尽管我同意它应该起作用。我遇到的一个大问题是$this->setTemplateDir(“”);出于某种原因将$template_dir转换为数组。我很确定这不应该发生。实际上我就是这样开始的,但它完全不起作用。尽管我同意应该这样做。我遇到的一个大问题是$this->setTemplateDir(“”);出于某种原因将$template_dir转换为数组。非常确定不应该发生这种情况。啊,谢谢。这绝对有道理,我只是认为由于复制/粘贴的代码不起作用而存在某种缺陷。啊,谢谢。这绝对有道理,我只是认为由于这个事实而存在某种缺陷复制/粘贴的代码不起作用。