Introduction to PHP-DI: What It Is and Why You Need It
\n\nPHP-DI (PHP Dependency Injection) is a powerful library for Dependency Injection (DI) in PHP applications. It implements the Inversion of Control (IoC) pattern and provides a container for automatically resolving dependencies between classes.
\n\nWhy do you need PHP-DI? In modern PHP projects (especially on frameworks like Laravel, Symfony, Slim), classes often depend on other classes. Without a DI container, you have to manually create all dependencies — this leads to:
\n- \n
- Tight coupling of code \n
- Difficulty in testing \n
- Code duplication when creating objects \n
- Problems with maintenance and extension \n
PHP-DI solves these problems by automatically determining which classes your code needs and creating them with the correct dependencies. The library supports:
\n- \n
- Automatic injection via constructor \n
- Defining dependencies through annotations (phpDoc) \n
- Configuration via arrays, YAML, or PHP files \n
- Lazy loading \n
- Integration with popular frameworks \n
Installing PHP-DI
\n\nPHP-DI is installed via Composer — the standard package manager for PHP:
\n\ncomposer require php-di/php-di\n\nIf you are using the Slim framework, install the integration package:
\n\ncomposer require php-di/slim-bridge\n\nFor Symfony, there is a separate package:
\n\ncomposer require php-di/symfony-bridge\n\nAfter installation, include the Composer autoloader in your project:
\n\nrequire __DIR__ . '/vendor/autoload.php';\n\nQuick Start with Code Example
\n\nLet's look at a simple example of using PHP-DI. Suppose we have a Mailer class that depends on a Logger class:
<?php\n// Logger class\nclass Logger {\n public function log($message) {\n echo "[LOG]: " . $message . "\\n";\n }\n}\n\n// Mailer class, which depends on Logger\nclass Mailer {\n private $logger;\n \n public function __construct(Logger $logger) {\n $this->logger = $logger;\n }\n \n public function send($to, $subject) {\n $this->logger->log("Sending email to {$to}: {$subject}");\n // Sending logic...\n }\n}\n\n// Create a DI container\n$container = new DI\\Container();\n\n// Get the Mailer object — all dependencies are resolved automatically\n$mailer = $container->get('Mailer');\n$mailer->send('user@example.com', 'Hello!');\n?>\n\nIn this example, PHP-DI automatically creates an instance of Logger and passes it to the Mailer constructor. You don't need to write new Logger() — the container does it for you.
Main Methods and Functions of PHP-DI
\n\n1. Container::get() — Getting an Object from the Container
\nWhat it does: Retrieves an object by its name (class, interface, or alias) from the container. If the object has not been created yet, the container creates it along with all its dependencies.
\nParameters:
\n- \n
string $name— the name of the class, interface, or alias \n
Example:
\n$container = new DI\\Container();\n$logger = $container->get('Logger'); // Creates and returns a Logger object\n$mailer = $container->get('Mailer'); // Creates a Mailer with Logger inside\n\n2. Container::set() — Registering a Definition
\nWhat it does: Registers a definition for a class, interface, or alias. Allows you to specify exactly how the object should be created.
\nParameters:
\n- \n
string $name— the name of the class or alias \n mixed $definition— desc