title: What design pattern can I use to emulate traits / mixins in PHP?
body: <p>Since traits aren't available in PHP 5.3 AFAIK I need to emulate some of the functionality they offer. Interfaces won't work because I need concrete functionality. </p>
  
  <h2>Problem:</h2>
  
  <p>I have two client classes that need to share some functionality but extend from different base classes.</p>
  
  <pre><code>ClassA extends Foo {}
  
  ClassB extends Bar {}
  </code></pre>
  
  <p>I need to be able to implement a function called getComponent() in both classes and it needs to be functionality identical.</p>
  
  <p>Changing the base class is not an option. I was thinking to do something like this:</p>
  
  <pre><code>Class ComponentHandler {
    function getInstance() {
      //return singleton
    }
  
    function getComponent() {
     //required functionality
    }
  }
  
  Class A extends Foo {
  
    var $handler;
  
    function __construct() {
      $this-&gt;handler = ComponentHandler::getInstance();
    }
  }
  </code></pre>
  
  <p>I would implement this constructor in both ClassA and ClassB. In my client I would make calls like this:</p>
  
  <pre><code>$class = new ClassA();
  $class-&gt;handler-&gt;getComponent();
  
  $class = new ClassB();
  $class-&gt;handler-&gt;getComponent();
  </code></pre>