Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Trait and an Abstract Class in PHP

I recently came across Traits in PHP and I'm trying to understand them. During my research I stumbled upon this Stack Overflow question: Traits vs. Interfaces. The accepted answer mentions the following:

An interface defines a set of methods that the implementing class must implement.

When a trait is use'd the implementations of the methods come along too--which doesn't happen in an Interface.

So far so good but this sounds exactly like the difference between an interface and an abstract class to me. So this raises a follow-up question for me:

  • What is the difference between a Trait and an Abstract Class in PHP?

I am aware that I can extend from only one abstract class and on the other hand use any amount of traits. But is this really the only difference? I still don't completely understand traits and its use.

like image 622
simon Avatar asked Sep 13 '16 09:09

simon


People also ask

What is difference between trait and class in PHP?

The main difference between the Traits and Interfaces in PHP is that the Traits define the actual implementation of each method within each class, so many classes implement the same interface but having different behavior, while traits are just chunks of code injected in a class in PHP.

What is difference between abstract class and inheritance in PHP?

PHP - Interfaces vs.All interface methods must be public, while abstract class methods is public or protected. All methods in an interface are abstract, so they cannot be implemented in code and the abstract keyword is not necessary. Classes can implement an interface while inheriting from another class at the same ...

What is a trait in PHP?

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

What is the difference between trait and interface?

An interface is a contract that says “this object is able to do this thing”, whereas a trait is giving the object the ability to do the thing. A trait is essentially a way to “copy and paste” code between classes.


2 Answers

Traits allow you to share code between your classes without forcing you into a specific class hierarchy. Say you want all your classes to have the convenient utility method foo($bar); without traits you have two choices:

  • implement it individually with code redundancy in each class
  • inherit from a common (abstract) ancestor class

Both solution aren't ideal, each with their different tradeoffs. Code redundancy is obviously undesirable, and inheriting from a common ancestor makes your class hierarchy design inflexible.

Traits solve this problem by letting you implement foo($bar) in a trait which each class can "import" individually, while still allowing you to design your class hierarchy according to business logic requirements, not language necessities.

like image 119
deceze Avatar answered Oct 05 '22 13:10

deceze


Not exactly... Let's quote official documentation for this purpose:

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.

So Traits are used for composition purposes to enable the class to perform some logic/behavior. If you're inheriting from another/abstract class, it's usually for purposes of polymorphism and you get a distinct inheritance/class hierarchy, which may or may not be desirable.

I think it all depends on the context, on the architecture and on what exactly are you trying to do.

like image 38
walther Avatar answered Oct 05 '22 12:10

walther