Traits vs. interfaces

前端 未结 13 1305
傲寒
傲寒 2020-11-28 17:09

I\'ve been trying to study up on PHP lately, and I find myself getting hung up on traits. I understand the concept of horizontal code reuse and not wanting to necessarily in

13条回答
  •  孤街浪徒
    2020-11-28 17:51

    The main difference is that, with interfaces, you must define the actual implementation of each method within each class that implements said interface, so you can have many classes implement the same interface but with different behavior, while traits are just chunks of code injected in a class; another important difference is that trait methods can only be class-methods or static-methods, unlike interface methods which can also (and usually are) be instance methods.

提交回复
热议问题