Dependency Injection Container
From phpBB Development Wiki
What is it?
A dependency injection container is an object that manages services and their creation. A service is an object that does something; it provides a service. Services can depend on other services, and the container has the job of wiring them up.
You can think of the container as "an array of objects on steroids". It knows about a number of objects and allows you to retrieve them. It will only create the objects you ask for, so it is lazy in a sense.
phpBB uses the Symfony2 Dependency Injection component.
In order to create them, the container needs to know about how services should be created. You do that by configuring it. phpBB uses a YAML configuration format to do this. All configuration files are within the config directory.
The entry point is services.yml. It imports some other files and then defines the top level services. Modifications to phpBB's core will likely involve changes to one of the service configuration files.
phpBB extensions are also able to define services. This is important if they want to provide cron tasks or any other service. By creating a config/services.yml file within the extension directory, that file will automatically get loaded into the container. See the page on dependencies for more information.
Example usage (PHP)
To access services, use get(service name), to access variables, use getParameter(parameter name)
See common.php and config/*.yml for more examples of usage.
- $request = $phpbb_container->get('request');
- $user = $phpbb_container->get('user');
- $auth = $phpbb_container->get('auth');
- $db = $phpbb_container->get('dbal.conn');
- $template = $phpbb_container->get('template');
- $phpbb_root_path = $phpbb_container->getParameter('core.root_path');
- $php_ext= $phpbb_container->getParameter('core.php_ext');
For further reading, check out the following resources: