简体   繁体   English

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

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

I am using the Smarty Template engine ( http://smarty.net ) on a website but I have additional, custom Functions to handle template compiling tasks. 我在网站上使用Smarty模板引擎( http://smarty.net ),但是我还有其他自定义函数来处理模板编译任务。

In order to be able to update the Smarty framework files (eg /smarty/Smarty.class.php) and not having to copy-paste my custom function into the Class inside Smarty.class.php, I thought "hey, let's just make my own Class and extend the Smarty Class!". 为了能够更新Smarty框架文件(例如/smarty/Smarty.class.php),而不必将自定义函数复制粘贴到Smarty.class.php中的Class中,我想:“嘿,让我们我自己的班级,并扩展Smarty班级!”。 However, I can't get this to work and would appreciate any enlightening advice :) 但是,我无法使它正常工作,不胜感激任何建议:)

This is what I got at the moment: 这是我现在得到的:

  • Web-Root/ 网络根/
    • includes/ 包括/
      • Smarty.inc.php Smarty.inc.php
    • smartylib/ smartylib /
      • Smarty.class.php Smarty.class.php
      • Smarty_Compiler.class.php Smarty_Compiler.class.php

Smarty.class.php Smarty.class.php

The third party vendor file and class that I 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 Smarty_Compiler.class.php

Another third party vendor file and class that I don't wanna touch: 我不想接触的另一个第三方供应商文件和类:

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.inc.php

My custom Smarty include file with new Subclasses and instantiating the $smarty object: 我的自定义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;

Result and problem 结果与问题

Okay I hope my intentions are clear from above code snippets: I want the object $smarty to contain also 好的,我希望我的意图可以从上面的代码片段中得到澄清: 我希望对象$ smarty也包含

  1. my additional, new custom template function, 我的其他新自定义模板功能
  2. and my replacement function 和我的替换功能

And because I use " extends " for my 2 classes declaration, I thought this should simply work by using: 并且由于我在2个类声明中使用了“ extends ”,因此我认为这应该可以通过以下方式简单地工作:

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

But unfortunately it doesn't :( I COULD add my custom functions directly into the Smarty Class inside Smarty.class.php - but this will, obviously, make the file non-updateable. 但不幸的是,它不是:(我可以将自定义函数直接添加到Smarty.class.php中的Smarty类中-但这显然会使文件不可更新。

What am I missing by using my 2 Subclasses extending the Smarty Class and while talking to the methods declared in them? 通过使用2个子类扩展Smarty类并与它们中声明的方法进行交谈,我缺少什么? Or how would you approach extending a third-party Class with custom/rewritten functions without touching the original code? 还是您将如何使用自定义/重写的功能来扩展第三方类而不接触原始代码?

Thank you for any advice! 感谢您的任何建议!

✅ Based on this stackoverflow-Question/Answers I was able to overcome my problem and getting the Code run with my custom Classes and Methods. on基于这个stackoverflow-Question / Answers,我能够克服我的问题,并使用自定义类和方法来运行代码。

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 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

If I still missed something or any recommendations, I'd be happy to hear about it :) 如果我仍然错过任何东西或任何建议,我很乐意听到:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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