【发布时间】:2016-04-10 00:05:36
【问题描述】:
我在网站上使用 Smarty 模板引擎 (http://smarty.net),但我有额外的自定义函数来处理模板编译任务。
为了能够更新 Smarty 框架文件(例如 /smarty/Smarty.class.php)并且不必将我的自定义函数复制粘贴到 Smarty.class.php 中的类中,我想“嘿,让我们创建自己的类并扩展 Smarty 类!”。但是,我无法让它发挥作用,并希望得到任何启发性的建议:)
这是我目前得到的:
- 网络根/
- 包括/
- Smarty.inc.php
- smartylib/
- Smarty.class.php
- Smarty_Compiler.class.php
- 包括/
Smarty.class.php
我不想接触的第三方供应商文件和类:
class Smarty {
// various vars declarations
public function __construct() {
// ...
}
function _compile_source($resource_name, &$source_content, &$compiled_content, $cache_include_path=null) {
// magic...
$smarty_compiler = new $this->compiler_class;
// more magic...
}
// more class methods declarations
}
Smarty_Compiler.class.php
另一个我不想接触的第三方供应商文件和类:
class Smarty_Compiler extends Smarty {
// various vars and methods declarations...
function _syntax_error($error_msg, $error_type = E_USER_ERROR, $file=null, $line=null) {
$this->_trigger_fatal_error("syntax error: $error_msg", $this->_current_file, $this->_current_line_no, $file, $line, $error_type);
}
// more various methods declarations...
}
Smarty.inc.php
我的自定义 Smarty 包含文件,其中包含新的子类并实例化 $smarty 对象:
/**
* My extensions for the Smarty Class
*/
Class MySmarty extends Smarty {
// different custom vars declarations
/**
* My ADDITIONAL custom Template Compile function
*/
public function compile ($template, &$errors) {
// custom code
}
}
/**
* My extensions for the Smarty_Compiler Class
*/
Class MySmarty_Compiler extends Smarty {
// different custom vars declarations
var $_manual_compiler_errors = array();
/**
* My REPLACEMENT _syntax_error function
*/
public function _syntax_error($error_msg, $error_type = E_USER_ERROR, $file=null, $line=null) {
global $_manual_compiler_errors;
if ($_manual_compiler_errors) {
array_push($_manual_compiler_errors, "smarty syntax error on line ".$this->_current_line_no.": $error_msg");
}else{
$this->_trigger_fatal_error("smarty syntax error: $error_msg", $this->_current_file, $this->_current_line_no, $file, $line, $error_type);
}
}
}
// Instantiate Smarty
include_once($_SERVER['DOCUMENT_ROOT'].'/smartylib/Smarty.class.php');
$smarty = new Smarty;
$smarty->debugging = true;
$smarty->force_compile = false;
结果与问题
好的,我希望从上面的代码 sn-ps 中可以清楚我的意图:我希望对象 $smarty 也包含
- 我的附加新自定义模板功能,
- 和我的替换函数
因为我使用“extends”作为我的 2 个类声明,我认为这应该可以通过使用:
$smarty->compile(...);
$smarty->_syntax_error(...);
但不幸的是,它没有 :( 我可以将自定义函数直接添加到 Smarty.class.php 内的 Smarty 类中 - 但这显然会使文件不可更新。
使用我的 2 个子类扩展 Smarty 类并与其中声明的方法进行对话时,我遗漏了什么? 或者,如果没有自定义/重写函数,您将如何处理扩展第三方类?触摸原始代码?
感谢您的任何建议!
【问题讨论】:
-
您仍在实例化基础 Smarty 类
$smarty = new Smarty;,而不是实例化您的扩展类$smarty = new MySmarty;,但这只是其中的一部分。从缩写代码中,不清楚如何使用MySmarty_Compiler类。这将需要通过名称进行类似的实例化。如果您可以提供有关如何使用它的更多详细信息,那么有人可以提供更完整的答案。 -
谢谢@MichaelBerkowski!我没有意识到我需要实例化的不是原始的“Smarty”类,而是我的新子类——到目前为止,它仍然有效。但是,现在我正在迷茫如何实例化第二个子类“MySmarty_Compiler”。我可以在“MySmarty”类__construct中添加
$smarty_compiler = new MySmarty_Compiler;之类的东西吗?似乎我还需要重新实例化这个$smarty_compiler对象,它最初是在 Smarty.class.php 的“Smarty”类中创建的。顺便提一句。我添加了 MySmarty_Compiler 类的代码......但不确定这是否有帮助。 -
Class MySmarty_Compiler extends Smarty...这应该是Class MySmarty_Compiler extends Smarty_Compiler对吧? -
@ByScripts 好吧,我不确定 - 我猜它实际上会更合乎逻辑,是的。如何通过实例化多个不同的子类(如 MySmarty 和 MySmarty_Compiler)正确扩展主类(Smarty)? :)
标签: php class inheritance smarty subclass