PHP code example of herrera-io / service-container

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

    

herrera-io / service-container example snippets




use Herrera\Service\Container;

$container = new Container(array('var' => 123));

$container['factory'] = $container->many(function () {
    return new ArrayObject(array('rand' => rand()));
});

$container['shared'] = $container->once(function() {
    return new ArrayObject(array('rand' => rand()));
});

echo $container['factory']['rand']; // echo "1197692050"
echo $container['factory']['rand']; // echo "995449132"
echo $container['shared']['rand']; // echo "89432412"
echo $container['shared']['rand']; // echo "89432412"



use Herrera\Service\Container;
use Herrera\Service\ProviderInterface;

class MyProvider implements ProviderInterface
{
    public function register(Container $container)
    {
        $container['hello'] = $container->once(function (Container $container) {
            echo 'Hello, ', $container['name'], "!\n";
        });
    }
}

$container =  new Container();
$container->register(new MyProvider(), array(
    'name' => 'Guest'
));