How PHP-DI works
Do you want to help out improving PHP-DI? Or are you simply curious? Here is a short presentation of how PHP-DI works.
The main component is the
Container class. It is created by a
ContainerBuilder, which is just a helper class.
It is the entry point from the user's point of view, it is also the component that coordinates all other sub-components.
Its main role is to return entries by their entry name:
$entry = $container->get('entryName');
A container instance has the following sub-components:
DefinitionSourcethat returns a
Definitionfor an entry name (by looking in several sub-definition sources)
- a list of
DefinitionResolverthat take a
Definitionand resolve it to a value (f.e. if it's an object, it will create it)
A definition defines what an entry is:
- a simple value (string, number, object instance…):
- a factory/callable returning the value:
- a definition of an entry alias:
- a definition of a class:
- a definition of an environment variable:
The class definition describes how the container should create a class instance (what parameters the constructor takes, …).