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?

怪我咯怪我咯2751 days ago1206

reply all(13)I'll reply

  • 天蓬老师

    天蓬老师2017-05-16 13:10:42

    Add a mark judgment at the function entrance. If the mark is true, follow the logic you wrote yourself. Otherwise, the original logic will remain unchanged. Examples are as follows:

    default() original, newFunc() new, main() public calling part

    main()
    {
      if ($flag) {
            NewFunc();
      }
      else
            Default();
    }
    
    或者直接修改default()
    default($flag){
      if ($flag) {
            你改的逻辑
      }
      else
            原来的
    }
    }

    This is the simplest modification. . .

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 13:10:42

    In this case, then this method is not public
    So, change this method, according to the actual situation,
    Add parameters, judge by parameters
    Re-differentiate the business logic, split the original method, and recombine it

    reply
    0
  • 曾经蜡笔没有小新

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

    Write the "public code" as "public class", and then change the organizational structure to the "mediator" model to reduce the coupling between the "public class" and other classes. Inherit the "public class" where changes are needed, and make modifications. part

    reply
    0
  • Cancelreply