Extending a PHP Class with Subclasses and instantiate all functions into one object -
i using smarty template engine (http://smarty.net) on website have additional, custom functions handle template compiling tasks.
in order able update smarty framework files (e.g. /smarty/smarty.class.php) , not having copy-paste custom function class inside smarty.class.php, thought "hey, let's make own class , extend smarty class!". however, can't work , appreciate enlightening advice :)
this got @ moment:
- web-root/
- includes/
- smarty.inc.php
- smartylib/
- smarty.class.php
- smarty_compiler.class.php
- includes/
smarty.class.php
the third party vendor file , class don't wanna touch:
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
another third party vendor file , class don't wanna touch:
class smarty_compiler extends smarty { // various vars , 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
my custom smarty include file new subclasses , instantiating $smarty object:
/** * extensions smarty class */ class mysmarty extends smarty { // different custom vars declarations /** * additional custom template compile function */ public function compile ($template, &$errors) { // custom code } } /** * extensions smarty_compiler class */ class mysmarty_compiler extends smarty { // different custom vars declarations var $_manual_compiler_errors = array(); /** * 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; result , problem
okay hope intentions clear above code snippets: i want object $smarty contain also
- my additional, new custom template function,
- and replacement function
and because use "extends" 2 classes declaration, thought should work using:
$smarty->compile(...); $smarty->_syntax_error(...); but unfortunately doesn't :( add custom functions directly smarty class inside smarty.class.php - will, obviously, make file non-updateable.
what missing using 2 subclasses extending smarty class , while talking methods declared in them? or how approach extending third-party class custom/rewritten functions without touching original code?
thank you advice!
✅ based on this stackoverflow-question/answers able overcome problem , getting code run custom classes , methods.
solution (summarized)
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 new way include_once($_server['document_root'].'/smartylib/smarty.class.php'); $smarty = new mysmarty; // instantiate subclass, loads main class $smarty_compiler = new mysmarty_compiler; // overwrite $smarty_compiler object second patched subclass, loads smarty_compile class if still missed or recommendations, i'd happy hear :)
Comments
Post a Comment