Ask about the difference between the principle of using class and the introduction of function directly.

ask a previous practice first

function me{
}

assume that this function is introduced at the beginning of every page
, but it is introduced

even though most pages do not need it.

and I write functions as classes + namespace to use
, but each page still needs to introduce the functionality of each class

class get {
 public static function me{
}
}

and the mvc I"ve seen recently decide which classes to introduce based on the router.
what are the advantages and disadvantages of these three? Does
have to be written in mvc to introduce the required functionality in the least redundant way?
or is there any other similar way to achieve the same effect?

charge

would you like to see if there will be more and more files in this way?
what if each file only does one or two things?
is this an open source?

Dec.28,2021

  • first of all, correct one point: MVC is just a structure of most WEB applications (a guiding principle for designing the framework). It itself and what you call the namespace namespace (specifically, it belongs to a new technology, new features) are two-dimensional things, so the two cannot be compared equally. In other words, most current frameworks use the principle of MVC to design code architecture, and namespace is a technology commonly used in the current PHP development process.
  • the first way you describe should, as I understand it, be the development of OPP (above is function , so it should not be encapsulated in the class). The encapsulation of classes can be regarded as the development way of OOP . And to achieve what you call "introducing the required functions in the least redundant way" actually has little to do with the two. no, no, no. When you split and combine methods (functions) in an appropriate way, you will always find the results you want to achieve. no, no, no. OPP you can only use require or include to load the files and functions you need, and the dependencies between files need to be maintained manually. However, under the general OOP , the dependencies between classes still need to be maintained manually, so there is no qualitative difference between them, so there is no specific pros and cons.
  • next, let's analyze your specific puzzles. the problem you throw out is actually so huge that it can be regarded as a subject that can be studied. No, no, no. In fact, the reverse of "introducing the required functionality in the least redundant way" is to "find the best way to improve the reuse of code and reduce redundancy". Many design patterns need to be combined with practical problems to achieve this goal. What you describe as "deciding which classes to introduce according to the router" seems to convey dependency injection (Denpdency Injection, DI). Dependency injection is essentially a call to a class, but it leaves the dependency of the class to the dependency injection container to manage. You can take a closer look at DIP , IoC and DI , and you should be able to understand it. In addition, you can also try to design Interface , abstract and then add trait to to achieve the effect you need.

it is obvious that direct reference will result in less code written and memory consumption, so execution efficiency is definitely the best.
but the biggest disadvantage is that after you have written 1000 function, you will find that your project can no longer be maintained, because you have to come up with 1000 function names.
actually, for php, without a framework, php is the most efficient, but without a framework, the code maintainability is too poor. This is the consideration of running efficiency and development efficiency


.

the maintainability and expansibility are not high, and it is very difficult to find the cause after the emergence of bug.

there is no silver bullet, and design patterns have been summed up by experienced developers over the years by implementing different requirements. The concept of class is one of them.


Direct functional programming is the most primitive way of writing PHP:
advantages: simple and direct
disadvantages: high maintenance costs for medium and large projects, which is not conducive to expansion

the way of class + include is the result of object-oriented development:

advantages: classes can isolate variables and methods
disadvantages: include files regardless of whether they are needed or not, which is a waste of resources, and in the case of introducing a third-party library, there is no guarantee whether the class name is duplicated

.

classes + namespaces + automatic import: the recommended development method, automatically loaded classes can be completely without their own include files, convenient for large and medium-sized project development and maintenance.

Menu