See some trait introduction, also see in the framework. But feel trait do things, with interface can also do?
Personally feel that each trait implementation of the functions are very concentrated, but also very light, this is very good, but some of the class may have to use a few trait at the same time, and if you inherit the class at the same time, the implementation of the interface, and it contains coverage, it is difficult to clarify the relationship between the class, This is my most confused place, of course, it may be I am not enough technology.
But since this grammar, there must be his reason, so I would like to ask, what is the meaning of using trait in the project?
Reply content:
See some trait introduction, also see in the framework. But feel trait do things, with interface can also do?
Personally feel that each trait implementation of the functions are very concentrated, but also very light, this is very good, but some of the class may have to use a few trait at the same time, and if you inherit the class at the same time, the implementation of the interface, and it contains coverage, it is difficult to clarify the relationship between the class, This is my most confused place, of course, it may be I am not enough technology.
But since this grammar, there must be his reason, so I would like to ask, what is the meaning of using trait in the project?
There are many purposes, and I believe the other answers will be mentioned from all angles.
One important purpose I know is to facilitate the work of the code generator .
If some of the code for a class is generated by the generator, and some of the code is written by itself, then we certainly want the code generated by the generator to trait
refer to the code we write ourselves trait
. This way, if the generator needs to be rerun, the self-written code does not need any change.
This may also be done with inheritance, but PHP does not inherit much. Referencing the code generated by multiple generators can only be relied upon trait
.
You can take a look at the partial class of C #. is very similar in purpose.
You can understand this.
1, trait
used to re-use the code
2, solve the php
problem of not much inheritance (multi-inheritance will also cause problems, not to expand here)
3, trait
the use of the class, and trait
itself is can not have any relationship
.......
trait
It defines the implementation
interface
Just a statement.
There's a difference between these two.
......
PHP
As with Java
most object-oriented languages, it is a single inheritance, and a class can inherit only one parent class. While single inheritance is a well-known way of making code clear and accurate, sometimes multiple inheritance has its own advantages. trait
is a way to resolve multiple inheritance scenarios. The benefit of multiple inheritance is that a class inherits the functionality of multiple parent classes, although this violates the principle of single-class responsibility, but in some cases it is most appropriate to use multiple inheritance for some small functions. For example, the Laravel
definition of a macro, it is trait
simple to let the class implement some C
macro-defined method, because it contains some entity methods and properties, so through the interface
definition is not possible, and the need to use this functionality of the class may have been inherited from other classes, so through abstract class
definition is also not possible, so this situation trait
can reflect its advantages.
More in the code that solves multiple inheritance
Reduce maintenance inconvenience caused by copy and paste
In a popular way, you can split a repeating method into a file, which is introduced by using use to achieve the purpose of code reuse.