PHPackages                             jersoncarin/container - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. jersoncarin/container

ActiveLibrary

jersoncarin/container
=====================

A lightweight DI Container

01PHP

Since Feb 25Pushed 5y ago1 watchersCompare

[ Source](https://github.com/jersoncarin/container)[ Packagist](https://packagist.org/packages/jersoncarin/container)[ RSS](/packages/jersoncarin-container/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (1)Used By (0)

Service Container
=================

[](#service-container)

#### A lightweight dependency injection service container for cool kids

[](#a-lightweight-dependency-injection-service-container-for-cool-kids)

Features
--------

[](#features)

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

Installation
============

[](#installation)

### Composer

[](#composer)

```
composer require jersoncarin/container
```

### Git Clone

[](#git-clone)

```
git clone https://github.com/jersoncarin/container.git

# and add manually
```

How to use
==========

[](#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
$container->set('key',User::class);

// Set a class instance object
$user = new User
$container->instance('key',$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
$container->once('key',Request::class);

// 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');
```

Examples
========

[](#examples)

```
// 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) {
    var_dump($user);
});

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

$container->set('user',User::class);

// 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;
$container->set('user',$user);

// Using instance (No Di apply);
$container->instance('user',$user);

// Binding once
// Note that this function is like set()
// but have differences you can see above
$container->once('user',User::class);

// 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
$container->make(Crush::Class);

// 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
$container->call([BestFriend::class,'sayHi']);
// Also you may pass a optional parameters
$container->call([BestFriend::class,'sayHi'],'param1','param2');

// Removing bindings from the container
$container->remove('user');
```

Extending
=========

[](#extending)

Service Container provides an [extendable](https://github.com/jersoncarin/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
    $this->call(...);
});

// 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
Container::method_name(...$params)

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

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

Author
======

[](#author)

- Jerson Carin

License
=======

[](#license)

### GPL-3.0

[](#gpl-30)

You may view the license file for other informations

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/d632ba5d9d544a26e57c1549ab630f52bdab50c6dcadd1826eccbf99fb4a833a?d=identicon)[jersnet](/maintainers/jersnet)

---

Top Contributors

[![jersoncarin](https://avatars.githubusercontent.com/u/63774442?v=4)](https://github.com/jersoncarin "jersoncarin (13 commits)")

### Embed Badge

![Health badge](/badges/jersoncarin-container/health.svg)

```
[![Health](https://phpackages.com/badges/jersoncarin-container/health.svg)](https://phpackages.com/packages/jersoncarin-container)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
