search

Home  >  Q&A  >  body text

php - Several functional modules share part of the code. I want to modify one of the functions. How to avoid destroying other functions?

Part of the code in several functional modules is common. If you want to modify one of the functions, you need to modify the common code. How to avoid damaging other functions when you are not sure which functions reference this code?

怪我咯怪我咯2814 days ago1271

reply all(13)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:10:42

    Abstract it
    Then write multiple methods to implement the abstract method, and different functional points implement different business logic.

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 13:10:42

    The easiest way. Copy it and quote it separately.

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:10:42

    For example, if you want to modify the A method of a public module, and there are other modules that also use A, you can write another A1 method instead of modifying A.

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 13:10:42

    You can overwrite the public part of the code of the subclass, and you can modify the function of one of them at will

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-16 13:10:42

    Create a category Override this method in the category Import the category in this file.

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 13:10:42

    Write a subclass to inherit this public method, encapsulate the parts that need to be modified into the subclass, and use this subclass for the target module.

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 13:10:42

    Create a new method. A way to package others.

    function pre() {}
    
    function now() {
        var result = pre()
        // todo sth
        // todo sth
        return newResult
    }

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:10:42

    If your modification changes the behavior of the original code, it is best to create a new one
    If the new code has a lot of overlap with the original code, you can consider extracting the reusable parts

    reply
    0
  • 某草草

    某草草2017-05-16 13:10:42

    Two ways:

    1. Add flag type parameters to the method, and perform different processing according to different flags in the method

    2. Rewrite one more method

    reply
    0
  • 黄舟

    黄舟2017-05-16 13:10:42

    Let’s modify that method by classifying it

    reply
    0
  • Cancelreply