DEV Community

Cover image for HateItOrLoveIt: Traits?
Adedoja Adedamola
Adedoja Adedamola

Posted on

HateItOrLoveIt: Traits?

What are traits?

What are traits?
My Team Leader, the SALK once described it as a Horizontal Inheritance with this simple analogy,

All Birds have feathers, but not all birds can fly.

In this analogy, flight is a trait. All birds inherit the characteristic to have feathers, but only some can actually fly

According to Horizontal Reuse for PHP RFC

Traits is 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.

In English, Traits simply allow for the reuse of methods, If there is a need to share functionality across multiple classes, you use traits. Using traits, methods can be reused freely across several different independent classes regardless of their class hierarchies.

Traits are similar to mixins in other languages like Ruby and JS.

Traits Use Case

Just like the analogy up top. We have classes Parrot and Ostrich that both inherit from the Class Bird. Both Class Parrot and Ostrich have feathers inherited from the Class Bird, but only Parrot can fly because it has the trait Flight.

class Bird
{
    private $type;

    public function __construct(string $type)
    {
        $this->type = $type;
    }

    public function feathers(): string
    {
        return "I am a " . $this->type . " and i have feathers";
    }
}

trait Flight
{
    public function fly(): string
    {
        return " - I can fly too";
    }
}
class Parrot extends Bird
{
    use Flight;

    public function __construct()
    {
        parent::__construct('Parrot');
    }
}
class Ostrich extends Bird
{
    public function __construct()
    {
        parent::__construct('Ostrich');
    }
}

$skiTheOstrich = new Ostrich();
echo $skiTheOstrich->feathers(); //returns: I am a Ostrich and i have feathers

$johnTheParrot = new Parrot();
echo $johnTheParrot->feathers();
echo $johnTheParrot->fly(); //returns: I am a Parrot and i have feathers - I can fly too

Advantages of Traits

  1. Allows for code reuse
  2. Unrestricted by inheritance hierarchies
  3. A class can have multiple traits

Disadvantages of Traits

  1. Encourages bloated classes
  2. Simply a “copy and paste” for code between classes.
  3. A crutch for lazy programming
  4. An entire class’ abilities are NOT in one location visually.

VERDICT???

Personally, I do not like traits, why? you ask.
I am a stickler for traditional software design principles and Trait allows for multiple Inheritance and I personally favor Composition over Inheritance. For me, a lot of trait in code is a sign of code smell.

Using traits, it is difficult to follow the code, because it has this functionality somewhere that you don't know how the class implements it and then you spy a "use XYZ" somewhere up and you are like haha!

I also believe its a bit weird to test. So for these reasons, even though I believe they have their own use cases!!!

I HATE

Verdict

As I sign off I say:

My Opinions are my own as of today (might change tomorrow) and not the views of my employer (or mine by tomorrow).

Have a contrary opinion or you wanna give me a high five, you can @Trussdamola

Top comments (0)