PHP code example of djuricmilos / container

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

    

djuricmilos / container example snippets


use Laganica\Di\Container;

$container = new Container();

use Laganica\Di\ContainerBuilder;

$builder = new ContainerBuilder();
$builder->useAutowiring(false);
$builder->useAnnotations(false);

$container = $builder->build();

use Laganica\Di\ContainerBuilder;
use function Laganica\Di\bind;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => bind(Service::class)
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);

use Laganica\Di\ContainerBuilder;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => Service::class
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);

use Laganica\Di\ContainerBuilder;
use Psr\Container\ContainerInterface;
use function Laganica\Di\closure;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => closure(static function (ContainerInterface $container) {
        return new Service($container->get(Dependency::class));
    })
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);

use Laganica\Di\ContainerBuilder;
use Psr\Container\ContainerInterface;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => static function (ContainerInterface $container) {
        return new Service($container->get(Dependency::class));
    }
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);

use Laganica\Di\ContainerBuilder;
use function Laganica\Di\factory;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => factory(ServiceFactory::class)
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);

use Laganica\Di\FactoryInterface;
use Psr\Container\ContainerInterface;

class ServiceFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container)
    {
        return new Service($container->get(Dependency::class));
    }
}

use Laganica\Di\ContainerBuilder;
use function Laganica\Di\alias;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => Service::class,
    'service-alias' => alias(ServiceInterface::class)
]);

$container = $builder->build();
$service = $container->get('service-alias');

use Laganica\Di\ContainerBuilder;
use function Laganica\Di\value;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    'count' => value(100)
]);

$container = $builder->build();
$count = $container->get('count');

use Laganica\Di\ContainerBuilder;
use function Laganica\Di\bind;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => bind(Service::class)
]);

$container = $builder->build();
$service = $container->make(ServiceInterface::class);

use Laganica\Di\ContainerBuilder;

class Service
{
    /**
     * @Inject
     * @var Dependency
     */
    private $dependency;
}

$builder = new ContainerBuilder();
$builder->useAnnotations(true);

$container = $builder->build();
$service = $container->get(Service::class);