About the use of php traits

problem description

has been doing interface development recently, and suddenly I have an idea:
encapsulate traits, for each interface method and then create an interface class. The function of the interface class is to combine the traits interface to form a complete interface. In this way, you only need to replace the corresponding traits during the iteration of the version update.
is this feasible? Or is there any drawback?

Php
Mar.30,2021

first of all, one of your ideas is very good, that is, using traits to reuse code, traits itself is designed to solve the problem that PHP cannot inherit much and reuse code

but your above design, I think of some questions:

  1. how do different versions of a service exist at the same time (do you maintain a different version of traits)
  2. how your class only inherits different functions, traits, then why do it directly with the corresponding base class, do it with traits? this question is worth thinking about
  3. in actual development, traits is more likely to act as some abstract common parts, such as an abstract class An and an abstract class B. they may want to share some methods or something at the same time. You can use traits
  4. .
  5. when you ask this question, what I feel on my side is that you need to think more about how to design classes and how to design your software architecture
Menu