PHP code example of alanvdb / dependency-container

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

    

alanvdb / dependency-container example snippets


use AlanVdb\Dependency\LazyContainer;

$container = new LazyContainer();

$container->add('service1', function() {
    return new Service1();
});

if ($container->has('service1')) {
    $service1 = $container->get('service1');
}

use AlanVdb\Dependency\IterableLazyContainer;

$container = new IterableLazyContainer();

$container->add('service1', function() {
    return new Service1();
});

$container->add('service2', function() {
    return new Service2();
});

foreach ($container as $serviceId => $service) {
    // Process each service
}

use AlanVdb\Dependency\Factory\ContainerFactory;

$factory = new ContainerFactory();

$lazyContainer = $factory->createLazyContainer();
$iterableLazyContainer = $factory->createIterableLazyContainer();