PHP code example of thruster / container

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

    

thruster / container example snippets




use Thruster\Component\Container;

$container = new Container();



use Thruster\Component\Container;

$values = [
	'render.engine' => function ($container) {
		return new RenderEngine($container->get('translation.engine'));
	},
	'translation.engine' => function () {
		return new TranslationEngine();
	}
];

$container = new Container($values);



use Thruster\Component\Container;

$container = new Container();

$container->has('render.engine'); // = false
$container->set('render.engine', function ($container) {
		return new RenderEngine($container->get('translation.engine'));
});

$container->has('render.engine'); // = true

$renderEngine = $container->get('render.engine');

$container->remove('render.engine');
$container->has('render.engine'); // = false



use Thruster\Component\Container;

$container = new Container();

isset($container['render.engine']); // = false
$container['render.engine'] = function ($container) {
		return new RenderEngine($container->get('translation.engine'));
};

isset($container['render.engine']); // = true

$renderEngine = $container['render.engine'];

unset($container['render.engine']);
isset($container['render.engine']); // = false



use Thruster\Component\Container;

$container = new Container();

$i = 1;

$value = function () use ($i) {
	return $i++;
}

$factoryValue = function () use (&$i) {
	return $i++;
}

$container->set('normal', $value);

$container->get('normal'); // = 1
$container->get('normal'); // = 1

$container->set('factory', $factoryValue);

$container->get('factory'); // = 1
$container->get('factory'); // = 2



use Thruster\Component\Container;
use Thruster\Component\ContainerProviderInterface;

$container = new Container();

$provider = new class implements ContainerProviderInterface {
	public function register(Container $container)
	{
		$container->set(....);
	}
};

$container->addProvider($provider);