sdfcloud / di
sdfloud\di 是一个强大且轻量级的 PHP 依赖注入库。
Requires
- psr/container: ^1.0
- ua1-labs/firebug: 2.*
Requires (Dev)
- ua1-labs/firetest: 2.*
This package is auto-updated.
Last update: 2024-09-18 14:30:48 UTC
README
A light weight PHP dependency injection library featuring automatic, constructor based, dependency resolution. When you need a new object from a class definition just simply ask for it. Any dependencies type hinted on the constructor will be resolved and injected before the class is instantiated into an object.
The whole reason for Sdfcloud/Di to exist is to manage and resolve dependencies for you. The Dependency Injection design pattern provides you with the ability to decouple class dependencies across your entire application by resolving these dependencies for you.
特点
- 自动基于构造函数的依赖解析。通过在构造函数中对要注入的对象进行类型提示,可以自动为您解析依赖。
- 循环依赖检测。
- 自动对象缓存。因此,当您多次请求一个对象时,它将直接提供,无需再次运行依赖解析过程。
- 在 DI 容器内定义您自己的依赖。这对于在单元/集成测试中模拟依赖非常有用。
- 定义您自己的依赖,以便在需要从容器外部获取具有特定依赖的类时使用。
文档
待定
使用 Composer 安装 Di
composer require sdfcloud/di
入门指南
假设您想获取一个实例化的对象 MyClass1。那么 MyClass1 需要你将其构造函数中的 MyClass2 传递进去。Sdfcloud/Di 将解析 MyClass2 并自动将其注入到 MyClass1 中,然后将实例化的对象 MyClass1 返回给您。
MyClass1
class MyClass1
{
public function __construct(MyClass2 $myClass2) {}
}
MyClass2
class MyClass2 {}
Di 实际应用
// instantiate di
$di = new Sdfcloud\Di();
// obtain an object and have it's dependencies resolved for you
$myClass1 = $di->get('MyClass1');