My class has a single dependency (Guzzle), so constructor looks like this:
public function __construct(Client $client)
{
$this->_client = $client;
}
However, user of this class will have to be aware of Guzzle and instantiate class like this:
$client = new \Guzzle\Http\Client();
$service = new Service($client);
I don't want end user to be concerned about this dependency, perhaps in the future even adding additional HTTP clients Service class could choose from. What would be the best practice to "hide" this dependency?
The DIC would take care of creating and injecting the dependency, the user would just get an instance of your service and not care how it was created.
However, that's easy to say, but you can't go and force your user to use a DIC. So another solution would be:
Example:
class MyServiceFactory
{
public function create(Client $client = null)
{
$client = $client ?: new Client(...);
return new MyService($client);
}
}