|
| 1 | +PHP-DI is a Container that makes [*Dependency Injection*](http://en.wikipedia.org/wiki/Dependency_injection) |
| 2 | +as practical as possible. |
| 3 | + |
| 4 | +PHP-DI also tries to avoid falling into the trap of the "Service Locator" antipattern and help you do *real* dependency injection. |
| 5 | + |
| 6 | +[](https://packagist.org/packages/mnapoli/php-di) [](https://packagist.org/packages/mnapoli/php-di) |
| 7 | + |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +* Simple to start with |
| 12 | +* Supports different configuration alternatives to suit every taste: |
| 13 | + * **Reflection**: zero configuration, intelligent guessing |
| 14 | + * **Annotations**: modern, practical and simple |
| 15 | + * **PHP code**: if you like complete control and auto-completion |
| 16 | + * **PHP array**: allows you to store it in a configuration file |
| 17 | + * **YAML**: elegant and concise |
| 18 | +* **Performances**: supports a large number of Caches |
| 19 | +* Lazy injection: lazy-loading of dependencies |
| 20 | +* Supports constructor injection, setter injection and property injection |
| 21 | +* Easy integration with any framework with [Injection over an existing instance](doc/inject-on-instance.md) |
| 22 | + |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +Let's go to the [Getting started guide](doc/getting-started.md)! |
| 27 | + |
| 28 | +And there is a [complete documentation](doc/) waiting for you. |
| 29 | + |
| 30 | + |
| 31 | +## What is dependency injection, and why use PHP-DI |
| 32 | + |
| 33 | +You can first read the [introduction to dependency injection with an example](doc/example.md). |
| 34 | + |
| 35 | +Dependency injection and DI containers are separate notions, and one should use of a container only if it makes things more practical (which is not always the case depending on the container you use). |
| 36 | + |
| 37 | +PHP-DI is about this: make dependency injection more practical. |
| 38 | + |
| 39 | +### How classic PHP code works |
| 40 | + |
| 41 | +Here is how a code **not** using DI will roughly work: |
| 42 | + |
| 43 | +* Application needs Foo (e.g. a controller), so: |
| 44 | +* Application creates Foo |
| 45 | +* Application calls Foo |
| 46 | + * Foo needs Bar (e.g. a service), so: |
| 47 | + * Foo creates Bar |
| 48 | + * Foo calls Bar |
| 49 | + * Bar needs Bim (a service, a repository, …), so: |
| 50 | + * Bar creates Bim |
| 51 | + * Bar does something |
| 52 | + |
| 53 | +### How Dependency Injection works |
| 54 | + |
| 55 | +Here is how a code using DI will roughly work: |
| 56 | + |
| 57 | +* Application needs Foo, which needs Bar, which needs Bim, so: |
| 58 | +* Application creates Bim |
| 59 | +* Application creates Bar and gives it Bim |
| 60 | +* Application creates Foo and gives it Bar |
| 61 | +* Application calls Foo |
| 62 | + * Foo calls Bar |
| 63 | + * Bar does something |
| 64 | + |
| 65 | +This is the pattern of **Inversion of Control**. The control of the dependencies is **inversed** from one being called to the one calling. |
| 66 | + |
| 67 | +The main advantage: the one at the end of the caller chain is always **you**. So you can control every dependencies: you have a complete control on how your application works. You can replace a dependency by another (one you made for example). |
| 68 | + |
| 69 | +For example that wouldn't be so easy if Library X uses Logger Y and you have to change the code of Library X to make it use your logger Z. |
| 70 | + |
| 71 | +### How code using PHP-DI works |
| 72 | + |
| 73 | +Now how does a code using PHP-DI works: |
| 74 | + |
| 75 | +* Application needs Foo so: |
| 76 | +* Application gets Foo from the Container, so: |
| 77 | + * Container creates Bim |
| 78 | + * Container creates Bar and gives it Bim |
| 79 | + * Container creates Foo and gives it Bar |
| 80 | +* Application calls Foo |
| 81 | + * Foo calls Bar |
| 82 | + * Bar does something |
| 83 | + |
| 84 | +In short, PHP-DI takes away all the work of creating and injecting dependencies. |
0 commit comments