用子类扩展PHP类,并将所有函数实例化为一个对象


Extending a PHP Class with Subclasses and instantiate all functions into one object

我正在使用Smarty模板引擎(http://smarty.net)在网站上,但我有额外的自定义函数来处理模板编译任务。

为了能够更新Smarty框架文件(例如/Smarty/Smarty.class.php),而不必将我的自定义函数复制粘贴到Smarty.classphp内的class中,我想"嘿,让我们制作自己的class并扩展Smarty class吧!"。然而,我无法做到这一点,如果有任何启发性的建议,我将不胜感激:)

这就是我现在得到的:

  • Web根/
    • 包括/
      • 智能公司.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...
}

智能公司.php

我的自定义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也包含

  1. 我的新增自定义模板功能,
  2. 和我的替换功能

因为我在2个类的声明中使用了"extends",所以我认为这应该通过使用来实现

$smarty->compile(...);
$smarty->_syntax_error(...);

但不幸的是,它没有:(我可以直接将我的自定义函数添加到Smarty.Class.php中的Smarty Class中,但这显然会使文件不可更新

使用我的2个子类扩展Smarty类,并与其中声明的方法进行对话,我缺少了什么或者,在不接触原始代码的情况下,如何使用自定义/重写的函数扩展第三方类?

感谢您的建议

✅基于这个堆叠式的问题/答案,我能够克服我的问题,并用我的自定义类和方法运行代码。

解决方案(总结)

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

如果我仍然错过了什么或任何建议,我很乐意听到:)