What is the meaning of trait in PHP?

Source: Internet
Author: User
Keywords Php
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

.......

traitIt defines the implementation
interface Just a statement.
There's a difference between these two.

......

PHPAs 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. traitis 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 classdefinition 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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.