PHP-DI
A PSR-11 dependency injection container for PHP that autowires your classes
Repository Health
Technical Analysis
PHP-DI is a dependency injection container designed to resolve and inject a class’s dependencies automatically wherever possible, so developers write plain PHP classes with typed constructor arguments and let the container figure out how to build the full object graph. It implements PSR-11 (ContainerInterface), making it a drop-in container for any framework or library that accepts a standard container.
Beyond autowiring, PHP-DI supports explicit PHP, YAML, or annotation-based definitions for cases autowiring can’t infer (interfaces, scalars, factories), and its CompiledContainer can pre-compile the container into optimized PHP code for production, trading configuration flexibility for near-zero runtime overhead.
What You Get
- Autowiring that resolves constructor dependencies from type hints with no configuration
- Explicit definition helpers (
DI\create(),DI\autowire(),DI\factory()) for cases autowiring can’t cover - PSR-11
ContainerInterfacecompliance so PHP-DI drops into any framework expecting a standard container - A
CompiledContainerthat precompiles container configuration into optimized PHP for production - PHP 8 attribute support (
#[Inject]) alongside array/YAML-based configuration
Common Use Cases
- Wiring service classes together in a framework-agnostic PHP application without a full framework’s built-in container
- Supplying a PSR-11 container to micro-frameworks like Slim that deliberately don’t bundle their own
- Managing complex object graphs (repositories, services, factories) in legacy codebases being modernized incrementally
- Compiling the container ahead of time in production to eliminate reflection-based autowiring overhead
Under The Hood
Architecture - PHP-DI’s Container.php and ContainerBuilder.php form the runtime core: the builder assembles definition sources (autowiring, PHP arrays, attributes) into a Container, which resolves entries lazily via Definition/ resolvers and Factory/ classes, with CompiledContainer.php and the Compiler/ namespace providing an alternate path that generates static PHP code for the same graph to skip reflection at runtime; Proxy/ supports lazy-loading of expensive services. Tech Stack - PHP 8+, PSR-11 (ContainerInterface) compliant, using PHP-DI’s own Invoker component for calling methods/functions with resolved parameters, packaged via Composer with PSR-4 autoloading rooted at src/. Code Quality - the tests/ directory contains roughly 161 test files against 62 source files, a notably high test-to-source ratio, and the project enforces phpstan and psalm static analysis in CI, though GitHub activity data shows low recent commit velocity, suggesting the library is in a stable maintenance phase rather than active feature development. API Design - the primary entry point (new Container() or ContainerBuilder) requires almost no boilerplate for the common case (autowiring), with explicit DI\create()/DI\autowire()/DI\factory() helpers providing an escape hatch that stays readable even in large configuration files.
Used by 3 apps in this directory
LimeSurvey
Forms Surveys
The world's most flexible open-source survey platform with 900+ templates, conditional logic, 80+ languages, and full GDPR compliance for any scale.
matomo
Analytics
Open-source, privacy-first web and app analytics that puts you in complete control of your data.
Typemill
CMS
Open-source flat-file CMS for publishing Markdown-based documentation, manuals, and eBooks without a database.