kittenphp/container

Simple Lightweight container

1.0.0 2018-01-18 20:22 UTC

This package is not auto-updated.

Last update: 2024-04-28 03:09:09 UTC


README

Introduced

This package is a lightweight service container.

install

composer require kittenphp/container

Example

1. Define the service

Each service is an ordinary PHP class that can provide some functionality:

class Service{
    public function show(){
        echo 'hello world! (kittenphp/container)';
    }
}

2. Registration service

use kitten\component\container\Container;
$c=new Container();
$c->set(Service::class,function (){
    return new Service();
});

3.Registering Shared Services

$c->share(Service::class,function (){
   return new Service();
});

4. Registering multiple services

class ServiceProvider implements ServiceProviderInterface{

    function register(ExpandContainerInterface $container)
    {
        $container->share(ServiceA::class,function (){
            return new ServiceA();
        });
        $container->share(ServiceB::class,function (){
            return new ServiceB();
        });
        $container->share(ServiceC::class,function (){
            return new ServiceC();
        });
    }
}
$c->addServiceProvider(new ServiceProvider());

5. Get Service

$c->get(Service::class)->show();