title: Are Factories Using An IoC Container A Service Locator?
body: <p>Lets say I have a factory returning different classes via methods. </p>
  
  <pre><code>class CarFactory
  {
      public function __construct(ContainerInterface $container) 
      {
          $this-&gt;container = $container;
      }
  
      public function createCarOne() : CarInterface
      {
          return $this-&gt;container-&gt;make(CarOneClass::class);
      }
  
      // Vs 
  
      public function createCarTwo() : CarInterface
      {
          return new CarTwoClass({Inject Dependencies Here});
      }
  }
  </code></pre>
  
  <p>When would this be considered a service locator or anti-pattern and why? I am considering the first method solely for the dependency resolution provided by the container. All car's have the same typed interface dependencies the main difference of the entities come from how they transform the data provided. </p>
  
  <p>Whenever one of these methods are called I need a new instance of the specified car so the data set can be transformed based on the choice. </p>
  
  <p>This is not the implementation but the easiest example I can provide. </p>
  
  <pre><code>$output = [];
  
  foreach ($car as $key =&gt; $data) {
      $newCar = $this-&gt;factory-&gt;createCar{$key}();
  
      // Pass Some Data To The New Car Methods So It Can Be Transformed 
  
      $output[] = $newCar;
  }
  return $output;
  </code></pre>
  
  <p>If this is the wrong approach what would be the alternative option?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>After further digging I see some IoC containers pass factory callables as dependencies. I was going to bind each <code>Car</code> to a callable but thanks to the ability to type hint data from method returns (php7) I can configure factories using a provider then call the 'callable factory' from within the <code>CarFactory</code>. Requires additional binding but prevents the need to reference/dependency inject the IoC container within every factory. </p>
  
  <p>Still researching I would love to hear feedback from those with more experience.</p>
  
  <p>Ex:</p>
  
  <pre><code>// Within Some Registered Provider
  // I Will Have To Wire Each Car
  $one = function() use ($app) {
      return $app-&gt;make(CarOne::class);
  };
  $two = function() use ($app) {
      return $app-&gt;make(CarTwo::class);
  };
  
  $app-&gt;bind(ICarFactory::class, function($app) use ($one, $two) {
      return $app-&gt;make($concrete, [$one, $two]);
  });
  
  
  // Car Factory Constructor
  public function __construct(callable $carOne, callable $carTwo) {
      $this-&gt;one = $carOne;
      $this-&gt;two = $carTwo;
  }
  </code></pre>
  
  <p>Since get methods are type hinted ( view original car factory ) an error is thrown when the returned item does not implement <code>CarInterface</code>, each factory method would just have to call the 'callable factory' ( something like this <code>return ($this-&gt;one)();</code>).</p>
  
  <p>I believe i solve my problem of outsourcing creation of dependencies ( avoiding creating within factory was bothering the hell out of me ) while still following 'best practices'. Still looking for advice if anyone has any to offer.</p>