1. Go to this page and download the library: Download kode/di library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kode / di example snippets
use Kode\DI\Container;
$container = new Container();
// 绑定单例
$container->singleton(LoggerInterface::class, FileLogger::class);
// 绑定原型
$container->prototype(Request::class);
// 获取实例
$logger = $container->get(LoggerInterface::class);
use Kode\DI\Attributes\Inject;
use Kode\DI\Attributes\Singleton;
#[Singleton]
class UserService
{
#[Inject]
private LoggerInterface $logger;
#[Inject(id: 'cache.ttl',
use Kode\DI\ContextualContainer;
// 在协程环境中自动隔离实例
ContextualContainer::setContainer($container);
// 每个协程拥有独立实例
ContextualContainer::resolve(DatabaseConnection::class);
use Kode\DI\ServiceProvider;
class DatabaseServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->singleton(DatabaseInterface::class, MySQLDatabase::class);
}
public function boot(): void
{
// 启动逻辑
}
}