| Start |
|---|
| Constructor Injection Objects will often depend on other objects. Instead of creating the dependency in the constructor, the dependency should be passed into the constructor as a parameter. This ensures there is not tight coupling between the objects, and enables changing the dependency upon class instantiation. This has a number of benefits, including making code easier to read by making the dependencies explicit, as well as making testing simpler since the dependencies can be switched out and mocked more easily. In the following example, Without dependency injection, the code would probably look similar to: Using In the above example, where we are using dependency injection instead, we could easily change to a different Logger if doing so became necessary. For example, we might use a Logger implementation that logs to a different location, or that uses a different logging format, or that logs to the database instead of to a file. |
| Setter Injection Dependencies can also be injected by setters. This is especially interesting when the core functionality of the class does not rely on the dependency to work. Here, the only needed dependency is the Note that when using setter injection, it's better to extend the functionality rather than replacing it. When setting a dependency, there's nothing confirming that the dependency won't change at some point, which could lead in unexpected results. For example, a To prevent this, we should add a dependency with setter injection, like so : Like this, whenever we'll use the core functionality, it won't break even if there is no logger dependency added, and any logger added will be used even though another logger could've been added. We're extending functionality instead of replacing it. |
| Container Injection Dependency Injection (DI) in the context of using a Dependency Injection Container (DIC) can be seen as a superset of constructor injection. A DIC will typically analyze a class constructor's typehints and resolve its needs, effectively injecting the dependencies needed for the instance execution. The exact implementation goes well beyond the scope of this document but at its very heart, a DIC relies on using the signature of a class... ... to automatically instantiate it, relying most of the time on an autoloading system (opens new window). If you are using PHP in version at least 5.5 and want to get a name of a class in a way that's being shown above, the correct way is the second approach. That way you can quickly find usages of the class using modern IDEs, which will greatly help you with potential refactoring. You do not want to rely on regular strings. In this case, the Instead, we set rules in the container, prior to object creation, that instructs how specific types should be instantiated if need be. This has a number of advantages, as a DIC can
If we define rules about how specific type needs to be managed we can achieve fine control over which types are shared, instantiated, or created from a factory. |
Read Article
https://develop.horje.com/learn/1434/reference