quetzal / dependency-injection
Quetzal 框架的依赖注入容器
v0.1.1
2020-12-20 17:00 UTC
Requires
- php: >=7.3.0
- phpunit/phpunit: ^9.0
- psr/container: ^1.0
This package is auto-updated.
Last update: 2024-09-21 01:17:06 UTC
README
特性
创建容器
use DI\DependencyInjection\Container;
$container = new Container();
定义服务
$container->set('service', function() {
return new stdClass();
});
$container->set(Profiler::class);
use DI\DependencyInjection\ContainerBag;
$containerBag = new ContainerBag();
$containerBag[Profiler::class] = Profiler::class;
获取服务
$container->get('service')
$containerBag[Profiler::class]
通过构造函数定义依赖
配置服务类。我们必须在 __construct
方法中将依赖设置为参数类型。
class Profiler implements IProfiler
{
private $dep1;
private $dep2;
private $dep3;
public function __construct(
ProfilerDependencyA $dep1,
ProfilerDependencyB $dep2,
ProfilerDependencyC $dep3
)
{
$this->dep1 = $dep1;
$this->dep2 = $dep2;
$this->dep3 = $dep3;
}
public function dump()
{
// TODO: Implement dump() method.
}
}