title: How to avoid clashing PHP traits used for dependency injection
body: <p>I'm finally getting around to exploring traits in PHP. The first place I thought I'd try it out is injection of config bits into classes. If I'm using DIC, I might have code like this in any class that needs a config object:</p>
  
  <pre><code>protected function SetConfig($config) {
      $this-&gt;config = $config;
  }
  
  protected $config;
  </code></pre>
  
  <p>This seems like a natural fit to traits to avoid having that boilerplate code all over the place, so I might create this:</p>
  
  <pre><code>trait Config {
      protected function SetConfig($config) {
          $this-&gt;config = $config;
      }
  
      protected $config;
  }
  </code></pre>
  
  <p>and then use it like so:</p>
  
  <pre><code>class Foo {
      use Config;
  
      public function __construct() {
          //can now use $this-&gt;config
      }
  }
  </code></pre>
  
  <p>That's great. Now let's say I want to create a second trait, say, for logging:</p>
  
  <pre><code>trait Logger {
      protected function SetLogger($logger) {
          $this-&gt;logger = $logger;
      }
  
      protected $logger;
  }
  </code></pre>
  
  <p>Which I can use like this:</p>
  
  <pre><code>class Foo {
      use Logger;
  
      public function __construct() {
          //can now use $this-&gt;logger
      }
  }
  </code></pre>
  
  <p>Also great. Now the problem comes if those two traits want to use each other. It seems quite reasonable that a logger class would need to have a config object injected, which means doing this:</p>
  
  <pre><code>trait Logger {
      use Config;
  
      protected function SetLogger($logger) {
          $this-&gt;logger = $logger;
      }
  
      protected $logger;
  }
  </code></pre>
  
  <p>But then things will break when another class uses both of these traits:</p>
  
  <pre><code>class Foo {
      use Config, Logger;
  
      public function __construct() {
          //want to use $this-&gt;config and $this-&gt;logger
      }
  }
  </code></pre>
  
  <p>This, of course, doesn't work because the config bits are effectively duplicated in Foo.</p>
  
  <p>I could just leave out the <code>use Config;</code> piece from the Logger trait, knowing that it'll be there in the end. But this feels weird to me as it creates a sort of external dependency. What if I want to use the Logger someplace that didn't already have the config trait? This solution also means I need to suffer my IDE (PhpStorm 8) warning me about unknown methods, and not offering autocompletion. I realize I could fix these problems in turn by using @method, but that's just putting lipstick on a pig, so to speak.</p>
  
  <p>I could also alias the config bits in Logger, but that's also problematic.</p>
  
  <p>All of this has a bit of a smell to it, but I haven't figured out yet whether that's because this is a new pattern for me or if it really is a stinky pattern. Either way, I'm not sure the best way to make this approach actually work.</p>
  
  <p>Any advice on the best way to solve this problem in traits? Or is it better to avoid traits for DIC shortcutting?</p>