PHP-DI

A PSR-11 dependency injection container for PHP that autowires your classes

Library
Composer
v7.1.1
2,772stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
58/100Fair
Development Activity4
Maintenance44
Community84
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
74/100Good
Architecture80
Code Quality78
Innovation65
Learning Curve72

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 ContainerInterface compliance so PHP-DI drops into any framework expecting a standard container
  • A CompiledContainer that 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search