symfony/dependency-injection
Symfony DependencyInjection standardizes and centralizes object construction with a powerful service container. Define services and parameters, manage autowiring and configuration, and optimize performance through compilation for cleaner, decoupled apps.
new operators, global singletons) with a maintainable, configurable system. Reduces technical debt in large codebases.PhpDumper) to eliminate runtime reflection overhead, improving cold-start times for CLI tools or serverless functions.#[AutowireCallable], #[Target]), intersection types, and Stringable improves compatibility with modern PHP while future-proofing the stack.Avoid if:
Consider alternatives if:
"Symfony’s Dependency Injection (DI) component is the backbone of modern PHP frameworks like Laravel, powering scalable applications at companies like Dropbox, Vimeo, and Symfony itself. By adopting this, we’ll:
new calls) and compiled containers (faster cold starts)."This replaces manual DI with a Swiss Army knife for object construction, giving us:
bind('App\Service', fn() => new App\Service($dep1, $dep2)). Just annotate classes, and Symfony handles it.// Register a logger service with a 'monolog' tag
$container->register('app.logger', Logger::class)
->addTag('monolog');
// Later, inject ALL loggers:
$container->autowire(TaggedIterator::class, ['monolog']);
#[AutowireCallable], #[Target], and Stringable types."Key architectural wins:
prod vs. dev databases) via config.// Before: Tight coupling
class OrderService {
private $db;
public function __construct() {
$this->db = new MySQLConnection(config('db'));
}
}
With this:
// After: Decoupled, testable
class OrderService {
public function __construct(private ConnectionInterface $db) {}
}
Container config (YAML):
services:
App\OrderService:
arguments:
$db: '@database.connection'
database.connection:
class: PDO
factory: ['App\DB\ConnectionFactory', 'create']
calls:
- [setTimeout, [30]]
```"
How can I help you explore Laravel packages today?