Using Constructor Injection
Using Constructor Injection, the problem presented with setter injection is solved in that, in
order for the object to be instantiated, the dependencies must be injected via the constructor. If
they aren’t, a very helpful, specific fatal error is thrown by PHP.
Providing that dependency would look something like this:
$controller
=
new
CustomerController(
new
CustomerRepository());
$customer
=
$controller
->
viewAction
();
The
CustomerController
is updated to require an instance of
CustomerRepository
be passed
along to the constructor. We use type-hinting here so that an actual instance of
CustomerRepository
is given, not just any variable.
Do'stlaringiz bilan baham: |