wptechnix/di-container
Composer 安装命令:
composer require wptechnix/di-container
包简介
A minimal, explicit PSR-11 dependency injection container for PHP.
README 文档
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
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-28