A lightweight DI Container

dev-master 2021-02-25 17:34 UTC

This package is auto-updated.

Last update: 2024-05-26 00:51:31 UTC


A lightweight dependency injection service container for cool kids


  • Autowiring
  • Automatic constructor injection
  • Support Closures/Callable and Class
  • Easy to use



composer require jersoncarin/container

Git Clone

git clone

# and add manually

How to use

// Create instance of the container
$container = new Bulk\Components\Container\Container;

// Setting to container

// Set a object to a container
// it can be a closure/callable or a class

// Set a class instance object
$user = new User

// Set a object to a container but once only
// it can be a closure/callable or a class
// Note Once a once method is make/get, 
// the same object instance will be returned on subsequent calls into the container

// Getting/Making from container

// Service Container implements PSR-11 Container Interface
// This will throw an exception if binding is not exist or has error thrown
$instance = $container->get('key');
// Return bool
$isExists = $container->has('key');

// make method accepts a parameters, it's just like get
// but the differents is if binding is not exist to the container
// it will automatically create and register to the container
$instance = $container->make('key',['param1','param2']);

// Calling a class or a closure/callable function
// This method call the callback or method from the class
// it will accept Closure/Callable function and method (PUBLIC)
// If you calling the class/method you can use 'User@show', or array base ['User::class','show'],
// [$this,'show'],[$instance,'show'], and a closure or a string callable function
// and it's accept a parameters
$callback = $container->call([User::class,'show'],'param1','param2');


// User class
class User

// Direct Configuration

// If a class has no dependencies or only depends on other concrete classes/interfaces, the container does not need to be instructed on how to resolve that class.
// call() automatically resolve the class if it's exist and inject to your method/function/closure parameters

$container->call(function(User $user) {

// Simple bindings
// Note you can bind with interfaces


// or using closure
$container->set('user',function() {
    return new User;

// and also it accept an instance object, although you can use instance()
$user = new User;

// Using instance (No Di apply);

// Binding once
// Note that this function is like set()
// but have differences you can see above

// Resolving

// Using PSR-11 Compliance Container Interface
$user = $container->get('user');

// Using make method
$user = $container->make('user');
// also you may passed param arguments
$user = $container->make('user','someparam1','someparam2');

// Automatic Injection or Autowiring
// You may typehint a class and it will automatically resolved from the container

class Crush
    public function __construct(User $user) {

// Make it

// Calling a callback or method from the class with DI applies
// call() method call the callback or a class method with injected parameters
// you may typehint the class and it will automatically resolved the class

class BestFriend
    public function __construct(Crush $crush) {
    public function sayHi(User $user) {

// Calling sayHi method from the BestFriend class
// Also you may pass a optional parameters

// Removing bindings from the container


Service Container provides an extendable method that you can extend the container

// You may use extend() method 
Container::extend('method_name',function() {
    // You can access container instance using $this

// you can call your method like the normal method
// Note you can't use $this on static call
// also you can pass a optional parameters

// Using static method

// Using instance method
$container = new Bulk\Components\Container\Container;

$myMethod = $container->method_name(...$params);


  • Jerson Carin



You may view the license file for other informations