How to solve problems with code duplication?

Hello! When I started to learn php, everywhere was urged to ensure that the code is in any case not duplicated... they Say you change something and have to crawl through the files and change the same everywhere, and somewhere forgotten...
But there was no mention of the other side of the coin...
How do you fight this situation? When you want to change this code to only one part of the app, and changing it only affected those of which I can't remember. In the end, everything breaks...
March 20th 20 at 11:50
3 answers
March 20th 20 at 11:56
SOLID.
DRY - hortsa practice, but not everywhere applicable. For example, you have two somewhat similar sushnosti, not the fact that they should inherit from a common parent or to integrate services that work with them. When writing tests DRY can be even anti practice.
March 20th 20 at 11:52
It is difficult to answer an ambiguous question.
But if in short.
When you want to modify this code only

This does not mean that you have all the unique code in a single function, method or object, there may be hundreds (as much as possible not dependent from each other) as a constructor or bulb United into one functional (S in the abbreviation SOLID) because functionality may be different then sostavlyayuschie (elements) can be combined differently and therefore when you want to change something, ideally, you enter in the method a length of about 20 lines and safely change without fear of something to break.
March 20th 20 at 11:54
There is a method getWhatever(), which is common to all.
You can in a specific class to implement your getWhatever () in which to write parent::getWhatever() and to modify the General result to the particular case.
getWhatever() should not be private and final

Find more questions by tags PHP