PHP code example of fastd / container

1. Go to this page and download the library: Download fastd/container 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/ */

    

fastd / container example snippets


use FastD\Container\Container;

$container = new Container();

// 注册类服务
$container->add('logger', Monolog\Logger::class);

// 注册闭包服务
$container->add('database', function() {
    return new PDO('sqlite::memory:');
});

// 注册配置数组
$container->add('config', [
    'app_name' => 'My Application',
    'debug' => true
]);

// 获取服务实例 (PSR-11 标准方法)
$logger = $container->get('logger');
$config = $container->get('config');

// 检查服务是否存在
if ($container->has('logger')) {
    $logger = $container->get('logger');
}

// 使用数组语法操作容器
$container['cache'] = RedisCache::class;

if (isset($container['cache'])) {
    $cache = $container['cache'];
}

unset($container['cache']);