使用子类扩展 PHP Class 并将所有函数实例化为一个对象
Extending a PHP Class with Subclasses and instantiate all functions into one object
我在网站上使用 Smarty 模板引擎 (http://smarty.net),但我有额外的自定义函数来处理模板编译任务。
为了能够更新 Smarty 框架文件(例如 /smarty/Smarty.class.php)并且不必将我的自定义函数复制粘贴到 Smarty 中的 Class 中。 class.php,我以为"hey, let's just make my own Class and extend the Smarty Class!"。但是,我无法让它工作,我将不胜感激任何有启发性的建议:)
这是我目前得到的:
- 网络根目录/
- 包括/
- Smarty.inc.php
- smartylib/
- 聪明class.php
- Smarty_Compiler.class.php
聪明class.php
我不想碰的第三方供应商文件和class:
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:
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 包含文件包含新的 Subclasses 并实例化 $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;
结果与问题
好的,我希望我的意图从上面的代码片段中清楚:我希望对象 $smarty 也包含
- 我附加的新自定义模板函数,
- 和我的替换函数
并且因为我使用“extends”作为我的 2 classes 声明,我认为这应该通过使用简单地工作:
$smarty->compile(...);
$smarty->_syntax_error(...);
但不幸的是它没有 :( 我可以将我的自定义函数直接添加到 Smarty 中的 Smarty Class 中。class.php - 但这显然会使文件不可更新。
使用我的 2 个 Subclasses 扩展 Smarty Class 并与其中声明的方法交谈时,我错过了什么? 或者如何您会在不触及原始代码的情况下使用 custom/rewritten 函数扩展第三方 Class 吗?
谢谢任何建议!
✅ 基于 this Whosebug-Question/Answers 我能够克服我的问题并使用我的自定义 类 和方法获得代码 运行。
解决方案(总结)
Class MySmarty extends Smarty {
...
}
Class MySmarty_Compiler extends Smarty {
function _syntax_error($error_msg, $error_type = E_USER_ERROR, $file=null, $line=null) {
...
}
}
// Instantiate Smarty the new way
include_once($_SERVER['DOCUMENT_ROOT'].'/smartylib/Smarty.class.php');
$smarty = new MySmarty; // Instantiate my Subclass, it loads the Main Class too
$smarty_compiler = new MySmarty_Compiler; // Overwrite the $smarty_compiler Object with my second patched Subclass, which loads the Smarty_Compile Class too
如果我仍然遗漏了什么或任何建议,我很乐意听到:)
我在网站上使用 Smarty 模板引擎 (http://smarty.net),但我有额外的自定义函数来处理模板编译任务。
为了能够更新 Smarty 框架文件(例如 /smarty/Smarty.class.php)并且不必将我的自定义函数复制粘贴到 Smarty 中的 Class 中。 class.php,我以为"hey, let's just make my own Class and extend the Smarty Class!"。但是,我无法让它工作,我将不胜感激任何有启发性的建议:)
这是我目前得到的:
- 网络根目录/
- 包括/
- Smarty.inc.php
- smartylib/
- 聪明class.php
- Smarty_Compiler.class.php
- 包括/
聪明class.php
我不想碰的第三方供应商文件和class:
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:
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 包含文件包含新的 Subclasses 并实例化 $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;
结果与问题
好的,我希望我的意图从上面的代码片段中清楚:我希望对象 $smarty 也包含
- 我附加的新自定义模板函数,
- 和我的替换函数
并且因为我使用“extends”作为我的 2 classes 声明,我认为这应该通过使用简单地工作:
$smarty->compile(...);
$smarty->_syntax_error(...);
但不幸的是它没有 :( 我可以将我的自定义函数直接添加到 Smarty 中的 Smarty Class 中。class.php - 但这显然会使文件不可更新。
使用我的 2 个 Subclasses 扩展 Smarty Class 并与其中声明的方法交谈时,我错过了什么? 或者如何您会在不触及原始代码的情况下使用 custom/rewritten 函数扩展第三方 Class 吗?
谢谢任何建议!
✅ 基于 this Whosebug-Question/Answers 我能够克服我的问题并使用我的自定义 类 和方法获得代码 运行。
解决方案(总结)
Class MySmarty extends Smarty {
...
}
Class MySmarty_Compiler extends Smarty {
function _syntax_error($error_msg, $error_type = E_USER_ERROR, $file=null, $line=null) {
...
}
}
// Instantiate Smarty the new way
include_once($_SERVER['DOCUMENT_ROOT'].'/smartylib/Smarty.class.php');
$smarty = new MySmarty; // Instantiate my Subclass, it loads the Main Class too
$smarty_compiler = new MySmarty_Compiler; // Overwrite the $smarty_compiler Object with my second patched Subclass, which loads the Smarty_Compile Class too
如果我仍然遗漏了什么或任何建议,我很乐意听到:)