Hi,
I’ve decided to make the Phemto injector object global by singleton so when I need an object in a Controller object I can do
$config = Injector::getInstance()->create(“IConfig”);
Is there any disadvatages to this and making the controller object constructor take an IConfig?
The specific reason now is that I’ve made my base Controller class take an IConfig and an ISecurity object but now when I create classes which extends Controller they are forced to take the objects in their constructors too and send it to the base classes. It would be great if the creators of the new controllers didn’t need to think about that.
EDIT: I guess one disadvantage is that you need to allways use DI in the tests too since you can’t just send an object into the constructor?
Hi…
Hm. Are the extra constructor parameters so bad? This always seems a small price to pay to me, compared with the extra scaffolding you will need elsewhere. It also means your classes no depend on Phemto (now reduced to a service locator). This will all work of course, but if you want to use your controllers in another application you will have to edit them all.
If you really are doing a lot of controller inheritance you could try this…
abstract class PaymentController {
private $payments;
function paymentGatewayIs($payments) {
$this payments = $payments;
}
}
Then inheritance won’t require repeating the parameters. Add this to your wiring…
$injector->forType('PaymentController')->call('paymentGatewayIs');
That should cover it. It does mean that your inheritance tree would like to be isomorphic with resources. As long as that assumption holds, you should be OK.
yours, Marcus
How far do you think you should take it. What if a class is only used in one function, should you still retrieve that class in the constructor?
That might be an indicator of your class doing too much. Doesn’t have to be, but it’s an indicator.