Skip to content

appspanel/fastd-container

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Object Container

Building Latest Stable Version Total Downloads Latest Unstable Version License

Simple DI Container

requirements

  • PHP >=7.0

installation

composer require "fastd/container" -vvv

usage

using method injection

$container = new FastD\Container\Container();

$container
    ->injectOn('date', new MethodInjection())
    ->withMethod('now')
    ->withArguments([
        new DateTime(),
    ]);

$obj = $container->get('date');

echo $obj->date; // (new DateTime())->format(DateTime::W3C)

using construct injection

$container = new FastD\Container\Container();

$container
    ->injectOn('date', ConstructorInjection::class)
    ->withConstruct()
    ->withArguments([
        new DateTime(),
    ]);

$date = $container->get('date');

echo $obj->date; // (new DateTime())->format(DateTime::W3C)

using closure

$container = new FastD\Container\Container();

$container->add('date', function () use ($container) {
    return new DateTime('now', $container->get('zone'));
});

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

$dateTime = $container->get('date');

echo $obj->date; // (new DateTime('now', new DateTimeZone("UTC")))->format(DateTime::W3C)

using DI

$container = new FastD\Container\Container();

$container->add('zone', new DateTimeZone('UTC'));

$container->injectOn('date', function (DateTimeZone $dateTimeZone) {
    return new DateTime('now', $dateTimeZone);
});

$dateTimeZone = $container->get('date'); // new DateTime('now', new DateTimeZone('UTC'));

Testing

phpunit

贡献

非常欢迎感兴趣,愿意参与其中,共同打造更好PHP生态,Swoole生态的开发者。

如果你乐于此,却又不知如何开始,可以试试下面这些事情:

联系

如果你在使用中遇到问题,请联系: bboyjanhuang@gmail.com. 微博: 编码侠

License MIT

About

FastD Container.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%