While OrangePill makes a good point, it seemed to me that I turned on too. I also tend to define my constructors with an explicit constructor, but I do not expect the required objects to be passed when the instance is created.
Sometimes you create an instance that retrieves data from a database or some Http request. In your case, the first example expects the transfer of three dependencies, but who says that you will always need all three of them?
Enter lazy loading. The code example below is quite long, but it (IMO) is worth a look. If I use the service, I do not want to download all the dependencies unless I am sure that I will use them. Therefore, I defined a constructor to create an instance in one of the following ways:
$foo = new MyService($configObj); $bar = new MyService($configObj, null, $dbObj);
If I wanted to run some kind of test, I can still add dependencies when building my instance, or I can rely on the test-config object, or I could use the setDb and setCurl too:
$foo->setCurl($testCurl);
Following the first method of building an instance, I can safely say that if I just call the getViaCurl method, the Db class will never be loaded.
The getViaDb method is more complex (like the getDb method). I do not recommend you work with similar methods, but it just shows you how flexible this approach can be. I can pass an array of parameters to the getViaDb method, which can contain a custom connection. I can also pass a boolean value that will control what I'm doing with this connection (use it only for this call or assign a connection to the MyService instance.
I hope this is not too incomprehensible, but I'm pretty tired, so I'm not too good at this ATM.
Here's the code, anyway ... it should be very clear.
class MyService { private $curl = null; private $db = null; private $conf = null; public function __construct(Config $configObj, Curl $curlObj = null, Db $dbObj = null) { $this->conf = $configObj;