[英]Extending a PHP Class with Subclasses and instantiate all functions into one object
我在網站上使用Smarty模板引擎( http://smarty.net ),但是我還有其他自定義函數來處理模板編譯任務。
為了能夠更新Smarty框架文件(例如/smarty/Smarty.class.php),而不必將自定義函數復制粘貼到Smarty.class.php中的Class中,我想:“嘿,讓我們我自己的班級,並擴展Smarty班級!”。 但是,我無法使它正常工作,不勝感激任何建議:)
我不想涉及的第三方供應商文件和類:
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
}
我不想接觸的另一個第三方供應商文件和類:
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 include文件帶有新的子類並實例化$ 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也包含
並且由於我在2個類聲明中使用了“ extends ”,因此我認為這應該可以通過以下方式簡單地工作:
$smarty->compile(...);
$smarty->_syntax_error(...);
但不幸的是,它不是:(我可以將自定義函數直接添加到Smarty.class.php中的Smarty類中-但這顯然會使文件不可更新。
通過使用2個子類擴展Smarty類並與它們中聲明的方法進行交談,我缺少什么? 還是您將如何使用自定義/重寫的功能來擴展第三方類而不接觸原始代碼?
感謝您的任何建議!
on基於這個stackoverflow-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
如果我仍然錯過任何東西或任何建議,我很樂意聽到:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.