Dependency injection container

dev-main 2024-09-24 23:34 UTC

This package is auto-updated.

Last update: 2024-10-25 00:03:01 UTC


README

build

The dependency injection container

This package makes it possible to quickly connect the DI-container to your project.

The container can be used both in automatic mode, using type hints in class constructors, and accept fine-tuning using providers.

Example automatically make instance

use Duyler\DI\Container;
use YourClass;

$container = new Container;

$yourClassObject = $container->get(YouClass::class);

Make instance with provider

class YourClass
{
    private MyClassInterface $myImplements;
    
    public function __construct(MyClassInterface $myImplements)
    {
        $this->myImplements = $myImplements;
    }
}
use Duyler\DI\Provider\AbstractProvider

class ClassProvider extends AbstractProvider
{
    public function bind(): array
    {
        return [
            MyClassInterface::class => MyImplemensClass::class,
        ];
    }
}
$container->addProviders([
    MyClassInterface::class => ClassProvider::class,
]);

$yourClassObject = $container->get(YourClass::class);

Make instance with bind

$container->bind([
    MyClassInterface::class => MyImplemensClass::class,
]);

$yourClassObject = $container->get(YouClass::class);