sportlog/di
Composer 安装命令:
composer require sportlog/di
包简介
A simple PSR-11 dependency injection container
README 文档
README
A simple PSR-11 dependency injection container.
Install via Composer
You can install sportlog/di using Composer.
$ composer require sportlog/di
Minimum PHP version required is 8.
How to use
<?php require 'vendor/autoload.php'; use Sportlog\DI\Container; // Given this class and interface: interface FooInterface { public function getFoo(): string; } class Foo implements FooInterface { public function __construct(private ?string $foo = 'foo') { } public function getFoo(): string { return $this->foo; } } // 1) You can simply get the instance via class id $container = new Container(); $foo = $container->get(Foo::class); echo $foo->getFoo(); // outputs: "foo" // 2) When working with interfaces you must set // the class id which shall be created $container = new Container(); $container->set(FooInterface::class, Foo::class); $foo = $container->get(FooInterface::class); echo $foo->getFoo(); // outputs: "foo" // 3) You can also use a factory. Parameters are // getting injected class Config { public function getFooInit(): string { return 'init-foo'; } } $container = new Container(); // Instance of Config will be injected to the factory $container->set( FooInterface::class, fn (Config $config) => new Foo($config->getFooInit()) ); $foo = $container->get(FooInterface::class); echo $foo->getFoo(); // outputs: "init-foo"
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-17