【问题标题】:Extending a PHP Class with Subclasses and instantiate all functions into one object使用子类扩展 PHP 类并将所有函数实例化为一个对象
【发布时间】:2016-04-10 00:05:36
【问题描述】:

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

为了能够更新 Smarty 框架文件(例如 /smarty/Smarty.class.php)并且不必将我的自定义函数复制粘贴到 Smarty.class.php 中的类中,我想“嘿,让我们创建自己的类并扩展 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 包含文件,其中包含新的子类并实例化 $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;

结果与问题

好的,我希望从上面的代码 sn-ps 中可以清楚我的意图:我希望对象 $smarty 也包含

  1. 我的附加新自定义模板功能,
  2. 和我的替换函数

因为我使用“extends”作为我的 2 个类声明,我认为这应该可以通过使用:

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

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

使用我的 2 个子类扩展 Smarty 类并与其中声明的方法进行对话时,我遗漏了什么? 或者,如果没有自定义/重写函数,您将如何处理扩展第三方类?触摸原始代码?

感谢您的任何建议!

【问题讨论】:

  • 您仍在实例化基础 Smarty 类 $smarty = new Smarty;,而不是实例化您的扩展类 $smarty = new MySmarty;,但这只是其中的一部分。从缩写代码中,不清楚如何使用MySmarty_Compiler 类。这将需要通过名称进行类似的实例化。如果您可以提供有关如何使用它的更多详细信息,那么有人可以提供更完整的答案。
  • 谢谢@MichaelBerkowski!我没有意识到我需要实例化的不是原始的“Smarty”类,而是我的新子类——到目前为止,它仍然有效。但是,现在我正在迷茫如何实例化第二个子类“MySmarty_Compiler”。我可以在“MySmarty”类__construct中添加$smarty_compiler = new MySmarty_Compiler;之类的东西吗?似乎我还需要重新实例化这个 $smarty_compiler 对象,它最初是在 Smarty.class.php 的“Smarty”类中创建的。顺便提一句。我添加了 MySmarty_Compiler 类的代码......但不确定这是否有帮助。
  • Class MySmarty_Compiler extends Smarty ...这应该是Class MySmarty_Compiler extends Smarty_Compiler 对吧?
  • @ByScripts 好吧,我不确定 - 我猜它实际上会更合乎逻辑,是的。如何通过实例化多个不同的子类(如 MySmarty 和 MySmarty_Compiler)正确扩展主类(Smarty)? :)

标签: php class inheritance smarty subclass


【解决方案1】:

✅ 基于this 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

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

【讨论】:

  • 与@ByScripts 的建议相反,它在第二个子类 MySmarty_Compiler 扩展 Smarty_Compiler 时不起作用 - 它仅在扩展 Smarty 类时才起作用,就像第一个自定义类一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-14
  • 1970-01-01
  • 2016-01-07
  • 1970-01-01
  • 2013-05-24
  • 1970-01-01
  • 2016-06-19
相关资源
最近更新 更多