Traits Versus Inheritance In PHP

9th February 2020

The other day I was conducting a code review and found that a developer had used a trait to give two classes the same group of utility methods. Whilst there was nothing wrong with this in terms of functionality, I asked the developer why they had chosen to use traits instead of inheritance. We eventually decided that an inheritance model would be better suited to the situation but I thought I would go through some of the thought processes here.

What Is A Trait?

A trait, if you weren't aware, is like a class, but you don't instantiate it directly. Traits are defined using the trait keyword and are otherwise quite like a class in structure.

The idea is that code is essentially copied into the class you want to use it in from the trait and the class acts like it had that code all along. For example, let's take a simple trait.

  1. trait Hello {
  2. protected $name = 'Bob';
  3.  
  4. public function sayHello() {
  5. echo 'Hello ' . $this->name;
  6. }
  7. }

To use this trait in the class you include it with the use keyword, but within the structure of the class.

  1. class Welcome {
  2. use Hello;
  3. }

Now, we can call the sayHello() method from the Hello trait as if it was a part of the Welcome class.

  1. $welcome = new Welcome();
  2. $welcome->sayHello(); // Prints 'Hello Bob'.

What Is PHP Class Inheritance?

Whilst I am talking about how traits are used, it makes sense to (very briefly) cover class inheritance. In PHP you can represent an object using a class. For example, let's say that we want to represent a square. We need to store the height and the width, have some way of adding these properties and also have a way of getting the area.

  1. class Square
  2. {
  3. public $width;
  4. public $height;
  5.  
  6. public function __construct($width, $height)
  7. {
  8. $this->width = $width;
  9. $this->height = $height;
  10. }
  11.  
  12. public function getArea()
  13. {
  14. return $this->width * $this->height;
  15. }
  16. }

If we wanted to represent a triangle then we could either create another class that is not connected to the Square at all, or we could create an abstract Shape class and inherit from what we need from that. A triangle has the same basic properties of our shape (width and height) so it makes sense to go down the inheritance route.

  1. abstract class Shape
  2. {
  3. public $width;
  4. public $height;
  5.  
  6. public function __construct($width, $height)
  7. {
  8. $this->width = $width;
  9. $this->height = $height;
  10. }
  11. }

By inheriting from this class we can represent the square and the triangle without having to copy a bunch of code. We do this using the extends keyword.

  1. class Square extends Shape
  2. {
  3. public function getArea()
  4. {
  5. return $this->width * $this->height;
  6. }
  7. }
  8.  
  9. class Triangle extends Shape
  10. {
  11. public function getArea()
  12. {
  13. return ($this->width * $this->height) / 2;
  14. }
  15. }

How Are Traits Used?

Traits are used quite commonly in the PHP world. They mainly wrap things like loggers and utility methods and are used to inject this functionality without having to rely on complex inheritance patterns.

For example, in the Drupal JSON API module there is a trait called ResourceIdentifierTrait. This is used by the module to add common methods like getId(), getTypeName(), and getResourceType() to any class that uses it. This is used within the module by the EntityAccessDeniedHttpException exception class, which already inherits the CacheableAccessDeniedHttpException class. This means that we can allow the EntityAccessDeniedHttpException class to know about that context that it's called in without having to alter the CacheableAccessDeniedHttpException class to know about that ID of the resource being denied, which won't always exist elsewhere in Drupal.

To my mind, traits are used to solve the fact that PHP doesn't support multiple inheritance. Thinking back to the Shape inheritance example, we might also want to represent the colour of the shape. Since colours can be stored in a variety of different ways we don't want to inject this into the shape object as that would pollute what the Shape class is representing as this would violate SOLID principles. We also can't create a Square class that inherits Shape and Colour at the same time as this isn't allowed in PHP.

The classic solution to multiple inheritance was to create a sub-inheritance class. For example, to allow all shapes to use Colours, we would extend Shape into a ShapeColour class and then extend that to a Square class. Although this works, it does create lots of strange relationships between objects.

The solution to this is to create a Colour trait.

  1. trait ColorTrait
  2. {
  3. protected $colour;
  4.  
  5. public function setColour($colour)
  6. {
  7. $this->colour = $colour;
  8. }
  9.  
  10. public function getColor()
  11. {
  12. return $this->colour;
  13. }
  14. }

We can then use this in the Shape class to allow all Shapes to have a Colour.

  1. abstract class Shape
  2. {
  3. use ColourTrait;
  4.  
  5. public $width;
  6. public $height;
  7.  
  8. public function __construct($width, $height)
  9. {
  10. $this->width = $width;
  11. $this->height = $height;
  12. }
  13. }

Now, when we create a Square, we can also set the Colour at the same time.

  1. $square = new Square(10, 10);
  2. $square->setColour('red');
  3. echo $square->getColour();

 

Add new comment

The content of this field is kept private and will not be shown publicly.