title: Best method for handling multiple interface implementations?
body: <p>Unfortunately I'm stuck here.</p>
  
  <p>Consider the following rudimentary examples:</p>
  
  <pre><code>interface ChargeInterface
  {
      public function charge($amount);
  }
  
  class BraintreeCharge implements ChargeInterface
  {
      public function charge($amount)
      {
          // braintree logic here
      }
  }
  
  class StripeCharge implements ChargeInterface
  {
      public function charge($amount)
      {
          // stripe logic here
      }
  }
  </code></pre>
  
  <p>So there's an interface for charging a payment method, and there are, in this example, two concrete classes which implement the interface.</p>
  
  <p>I'd like to be able to decide on runtime which implementation should be used. So I thought I'd achieve this with custom factory classes:</p>
  
  <pre><code>class PaymentFactory
  {
      public static $implementation;
  
      public static function charge()
      {
          return $implementation::charge();
      }
  }
  
  class StripeFactory
  {
      public static function charge()
      {
          return new StripeCharge();
      }
  }
  
  class BraintreeFactory
  {
      public static function charge()
      {
          return new BraintreeCharge();
      }
  }
  </code></pre>
  
  <p>Than I could just use the factories:</p>
  
  <pre><code>PaymentFactory::$implemention = StripeFactory::class;
  $payments = PaymentFactory::charge();
  $payments-&gt;charge(100);
  </code></pre>
  
  <hr>
  
  <p>Another idea was to use a singleton based logic:</p>
  
  <pre><code>class PaymentFactory extends Singleton
  {
      protected $implementation;
  
      // Singleton logic missing in this example
  
      public function useImplementation($class)
      {
          $this-&gt;implementation = $class;
      }
  
      public function getImplementation()
      {
          return $this-&gt;implementation;
      }
  
      public static function charge()
      {
          $instance = self::getInstance();
          return new $instance-&gt;getImplementation();
      }
  }
  </code></pre>
  
  <p>Later ...</p>
  
  <pre><code>PaymentFactory::getInstance()-&gt;useImplementation(StripeCharge::class);
  
  $payments = PaymentFactory::charge();
  $payments-&gt;charge(100);
  </code></pre>
  
  <hr>
  
  <p>Do you've any suggestions regarding best practices here?
  I think I'd favour the first one, since the real implementation consists of more than just one class per package, as outlined in the example.
  Also it seems to me, this would be the more cleaner way.</p>