Dave Hulbert's Today I Learned (TIL)

Traits with interfaces

date: 2014-12-15 21:14:07

In PHP, Traits can't implement interfaces. They can, however, include abstract methods. These methods then need to be implemented by the concrete class that implements the trait. This allows a trait to provide some boilerplate implementation that depends on an concrete implementation. This isn't always a good idea (it's verging on violating the Dependency Inversion Principle but it's better than a trait attempting to use a method that has no contract.


trait LoggerTrait
    abstract public function log($level, $message, array $context = array());
    public function error($message, array $context = array())
        $this->log(LogLevel::ERROR, $message, $context);