繁体   English   中英

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

[英]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班级!”。 但是,我无法使它正常工作,不胜感激任何建议:)

这是我现在得到的:

  • 网络根/
    • 包括/
      • 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 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类中-但这显然会使文件不可更新。

通过使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM