psr/container
The PSR-11 Container Interface defines core abstractions for dependency injection in PHP. It’s a standard abstraction, not an implementation—used by DI containers like Laravel’s Illuminate\Container. Essential for dependency management, ensuring interoperability across container implementations.
The psr/container package provides only the interface definitions for PSR-11 — it’s not a dependency injection container itself. To get started:
composer require psr/container (most modern DI containers already require it transitively).Psr\Container\ContainerInterface. If consuming one (e.g., Laravel’s resolver, Symfony’s Container, Symfony DI, PHP-DI), you don’t need to install this directly — it’s a dependency of your chosen container.Psr\Container\ContainerInterface in your services or libraries to accept any PSR-11-compliant container, enabling decoupling and interoperability.app()->make(ContainerInterface::class) (via illuminate/container’s adapter), or leverage Container::add('psr/container', ...).ContainerInterface instead of concrete containers (e.g., Symfony, PHP-DI) to maximize compatibility.ContainerInterface easily in unit tests (e.g., Mockery::mock(ContainerInterface::class)) without pulling in full container implementations.psr/container 2.x requires PHP ≥ 7.4 (due to ContainerExceptionInterface extending Throwable). For older PHP, use psr/container 1.0–1.2 (but avoid — outdated and insecure).symfony/service-contracts, php-di/php-di, container-interop/container-interop) must be installed separately.psr/container ≥ 2.0 for has() to return bool and $id as string.ContainerExceptionInterface, remember it is a Throwable — always pair get() with try/catch, especially when resolving optional dependencies.ContainerInterface everywhere — prefer explicit constructor injection for better testability. Reserve ContainerInterface for adapter layers, factories, or dynamic resolution scenarios (e.g., plugin systems).How can I help you explore Laravel packages today?