PHP code example of raylin666 / container

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

    

raylin666 / container example snippets




aylin666\Container\Container;
use Raylin666\Container\ContainerFactory;

$container = ContainerFactory::getContainer();

// 绑定容器
$container->bind(DateTime::class, DateTime::class);

// 是否有绑定该容器
$container->has(DateTime::class);

// 实例化容器
$container->make(DateTime::class, ['timezone' => new DateTimeZone('UTC')]);

$container->bind('datetimezone', function () {
    return new DateTimeZone('UTC');
});

// 获取容器, 优先查看是否有已实例化的容器, 如果有则直接取出, 如果没有则实例化容器(make)并返回
$container->get('datetimezone');
// 为容器设置别名, 一般情况下该别名可用来作为装饰者, 因为本身就是装饰器设计模式, 比如 laravel 的 alias 就类似该原理
$container->alias('tzone', 'datetimezone');

$container->get('tzone');

$container->bind(DateTimeZone::class, DateTimeZone::class);

$container->make(DateTimeZone::class, ['timezone' => 'PRC']);

// ... 更多功能可阅读源码