apinephp/container

A minimalistic PSR-11 Dependency Container

2.0.0 2019-12-13 09:19 UTC

This package is auto-updated.

Last update: 2024-11-14 02:45:41 UTC


README

A minimalistic PSR-11 DI and IoC Container

Installation

Installation is made with composer

composer require apinephp/container

The package requires PHP 7.4 or newer.

Usage Example

<?php
    
use Apine\Container\Container;

$container = new Container();
$container->register('service', function () {
    return 'A service';
});

$container->get('service'); // 'A service'

DI with services

Instantiate new services using the createInstance function. It produces a Closure that will create an instance of the class applying dependency resolution and injection against the services in the container.

<?php
    
use Apine\Container\Container;
use function Apine\Container\createInstance;

$container = new Container();
$container->register('service', createInstance(StubClass::class));

$service = $container->get('service'); // Returns an instance of StubClass