wptechnix / di-container
A minimal, explicit PSR-11 dependency injection container for PHP.
v1.0.0
2026-06-27 09:58 UTC
Requires
- php: ^8.0
- psr/container: ^1.1 || ^2.0
Requires (Dev)
- phpstan/phpstan: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.10 || ^4.0
Provides
- psr/container-implementation: 1.0 || 2.0
This package is auto-updated.
Last update: 2026-06-28 08:05:34 UTC
README
A minimal, explicit PSR-11 dependency injection container for PHP. No autowiring, no reflection, no annotations, no magic — what you register is exactly what you get.
Requirements & Installation
composer require wptechnix/di-container
- PHP 8.0+
- PSR-11 compliant (
psr/container ^1.1 || ^2.0)
Quick Start
use WPTechnix\DI\Container; $c = new Container(); // Register a singleton — factory runs once, instance is cached $c->singleton('logger', fn() => new Logger()); $logger = $c->get('logger'); // same instance every time // Register a factory — factory runs on every resolution $c->factory('mail', fn() => new Mailer()); $mail1 = $c->get('mail'); // fresh instance $mail2 = $c->get('mail'); // another fresh instance
The Three Resolver Forms
// Closure — full control, receives container + resolved params $c->singleton('db', fn(Container $c) => new Connection($c->get('config'))); // String — class name, validated at registration time $c->singleton('logger', Logger::class); // Null — identifier IS the class name (shorthand) $c->singleton(Logger::class);
Feature Overview
| Feature | What it does | Learn more |
|---|---|---|
| Singletons & factories | Shared instance vs fresh per resolution | Singletons and Factories |
| Resolver forms | Closure, class string, null shorthand | Resolvers |
| Constructor parameters | Positional args + optional service resolution | Constructor Parameters |
| Aliases | Bind one name to another; the interface-binding pattern | Aliases |
| Tagged services | Group services; resolve them as a collection | Tagged Services |
| Service decoration | Wrap or replace a service before resolution | Extending Services |
| Service providers | Two-phase modular bootstrapping | Service Providers |
| PSR-11 | Interoperates with any PSR-11-aware framework | Container Lifecycle |
Learn More
For a guided tour, read the documentation in this order:
- Singletons and Factories — the fundamental registration choice
- Resolvers — the three ways to tell the container how to build a service
- Constructor Parameters — passing values and service dependencies to constructors
- Aliases — the interface-binding pattern
- Tagged Services — grouping related services for bulk resolution
- Extending Services — decorating services you don't own
- Service Providers — modular bootstrapping with two-phase registration
- Container Lifecycle — lazy initialization, resolution stack, boot freeze
- Exceptions — when things go wrong (and how to prevent it)
- API Reference — complete method signatures
License
MIT