# Dependency Injection (PHP-DI)

Automated testing is great, but whether it’s easy to write or not depends on your code design. As a rule of thumb, the more loosely coupled your classes are, the easier they are to test. And one can’t talk about loosely coupled classes and not mention [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection).

One simple example should clarify how it works. Imagine a class that encountered an error and wants to log it. Let’s also assume you’re using [Monolog ](https://packagist.org/packages/monolog/monolog)to create loggers that handle these events. Without dependency injection, whenever the class encounters an error it must now instantiate a logger and use it. If you have 50 classes that use loggers, you would write the code for instantiating a logger 50 times in 50 places (well, you’d probably write a function somewhere to simply retrieve an instantiated logger and that too would be a sort of dependency injection, in a nutshell).

With dependency injection, you would create a logger at some point in time and then simply pass it along to each class that can log errors (traditionally, in the constructor or through a method like `set_logger`). That way your classes don’t actually know what a logger is or how it works or how to get one — they simply get one passed along.

Normally you wouldn’t even bind yourself to any specific logger but you would use an interface. Luckily, for loggers, there already exists the [PSR-3](https://www.php-fig.org/psr/psr-3/) standard (which Monolog also implements). So your classes simply expect a PSR-3 `LoggerInterface` object passed on in the constructor.

When you write automatic tests for your classes, you don’t need to actually load Monolog too! You can just pass on any PSR-3 implementing class, or just use [Mockery ](https://github.com/mockery/mockery)to create one on the fly.

Dependency injection containers usually provide an array of features that are much more powerful than just that (e.g., auto-wiring or on-the-fly instantiation). For our projects, we use [PHP-DI](https://php-di.org/) but this is not actually a requirement for using our framework. The framework accepts any [PSR-11](https://www.php-fig.org/psr/psr-11/)-compatible container and only assumes the features that PSR-11 foresees.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://framework.deep-web-solutions.com/key-concepts-and-dev-tools/dependency-injection-php-di.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
