我个人的观点是,在编写简洁的代码时实际上很少使用特质。
与其使用特征将代码入侵到类中,不如通过构造函数或setter传递依赖项:
class ClassName { protected $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } // or public function setLogger(LoggerInterface $logger) { $this->logger = $logger; }}我发现比使用特征更好的主要原因是,通过消除与特征的硬耦合,您的代码更加灵活。例如,您现在可以简单地传递另一个记录器类。这使您的代码可重用和可测试。



