LoggerTrait: The Good (or Bad) approach to Logging

Luís Nóbrega
4 min readJan 5, 2020
A log. Image from PublicDomainPictures by Pixabay.

What is a Trait?

Generically, a Trait is a concept used in object-oriented programming, that allows for several methods to be used and, in this way, to extend the functionality of a class.

They allow incorporating inside another class any group of methods, inside some categorization, the Trait.

Similar to an abstract class, but with a great difference: it cannot be instantiated on its own.

In PHP they were introduced to be an instrument for code reuse because PHP is a single inheritance language.

And in PHP they have the following characteristics:

  • You can define properties.
  • You can define constructors.
  • You can define abstract methods.
  • It does not support inheritance.
  • Traits are not types.
  • It is not possible to instantiate on its own.

Is a Good or Bad practice?

There is a lot of controversy about Traits, being considered by many, like a bad practice.

Benjamin Eberlei on his blog considered him as static access. Others, that they are a kind of automated copy-paste code.

--

--

Luís Nóbrega
Luís Nóbrega

Written by Luís Nóbrega

Web Developer at 25friday. Likes to travel, write and being a father.

No responses yet