Edit this page

Container Component


Kraken container is both powerful dependency injection container and service container.


Container features:

  • Support for binding objects, classes, params and factory methods to container
  • Support for modifying container and removal of its definitions on fly
  • Autoresolve for not defined classes or servies
  • Autowiring for simple and nested dependencies
  • Service providers with configurable requirements and providees
  • Service registers
  • Sorting algorithm to ensure right execution order of providers, based on their dependencies
  • Kraken Framework compatibility


Dependency Injection Container

Dependency injection container is an object that knows how to instantiate and configure objects used throughout application and all of their dependencies.

Service Container

Service container is special case of dependency injection container that treats all of its bindings as abstract structures referred to as "services". Service container allows to create both concrete and virtual services. It is usually used together with set of service providers which it can manage, register, boot, sort and other things.

Service Provider

Service provider is object which main purpose is to provide information how given service should be registered and booted into service container.


Service is an abstract concept that usually referers to an object which is created for a specific purpose and is used throughout your application whenever you need the specific functionality it provides.


Binding Definition or Interface

$container = new Container();
$foo = new Foo();

$container->bind(FooInterface::class, $foo);

Binding Factory Method

$container = new Container();

$container->bind(FooInterface::class, function() {
    return new Foo();

Binding Default Params

$container = new Container();

$container->wire(FooBar::class, [

Creating Aliases

$container = new Container();

$container->alias(FooInterface::class, Foo::class);

Sharing Objects

$container = new Container();

// this will make Foo a singleton

Auto Wiring

$container = new Container();

// this
$bar = new Bar()
$foo = new Foo($bar);

// can be autoresolved by container using
$foo = $container->make(Foo::class);

Removing Definitions

$container = new Container();

$container->bind(Foo::class, $foo = new Foo());
$container->make(Foo::class); // === $foo

$container->make(Foo::class); // !== $foo

Writing Service Provider

class CustomProvider extends ServiceProvider implements ServiceProviderInterface
    protected $requires = [

    protected $providers = [

    protected function register(ContainerInterface $container)
        $loop = $container->make(LoopInterface::class);
        $wamp = new WampServer($loop);

        $container->bind(WampServerInterface::class, $wamp);

    protected function unregister(ContainerInterface $container)

    protected function boot(ContainerInterface $container)
        // Since boot method is fired after everything has been registered, you dont need to put services used here in $requires field.
        $runtime = $container->make(RuntimeContainerInterface::class);
        $wamp    = $container->make(WampServerInterface::class);

        $runtime->onStart(function() use($wamp) {
        $runtime->onStop(function() use($wamp) {

Using Service Provider

$container = new Container();
$register  = new ServiceRegister($container);

$register->registerProvider(new AProvider());
$register->registerProvider(new BProvider());


Important Classes & Interfaces

This section contains list of most important classes and interfaces shipped with this component. It does not include all classes and interface.


class Container implements ContainerInterface

This class represents concrete implementation of dependency injection container.


interface ContainerInterface extends ContainerReaderInterface, ContainerWriterInterface


interface ContainerAwareInterface

Interface for container aware objects. Its main purpose is to provide setter and getter methods for setting and obtaining container instance within objects composition.


class ServiceProvider implements ServiceProviderInterface

Abstract service provider which should be extended by each provider used together with service container.


interface ServiceProviderInterface


class ServiceRegister implements ServiceRegisterInterface

This class uses dependency injection container instance and behaves as service container


interface ServiceRegisterInterface
Start from the Beginning