PHPackages                             gravatalonga/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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. gravatalonga/container

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

gravatalonga/container
======================

Implementation of PSR-11 Container, it is lightweight yet powerful. Feature such as Lazy Factory, Factory, Optinal Share of Services, Array Access and other it can be very versatile.

1.8.4(3y ago)52.6k↑50%11MITPHPPHP ^8.0|^8.1

Since Apr 18Pushed 3y ago1 watchersCompare

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

READMEChangelogDependencies (6)Versions (34)Used By (1)

[![Container](Container.jpg)](Container.jpg)

Container
=========

[](#container)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c657270887dba5468b57fb0bc5f408124837f58c8896828b462960ca2cf8e820/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f677261766174616c6f6e67612f636f6e7461696e65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gravatalonga/container)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/0bc84bfe073e929dfc290f2e65bdc394f6496f2b291d537fe4e2ef183abc53bc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f677261766174616c6f6e67612f636f6e7461696e65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gravatalonga/container)[![Coverage Status](https://camo.githubusercontent.com/f62f85a62758210abeaaac18ccf7367b38b802071a91ee87fe01019171d3c7f0/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f677261766174616c6f6e67612f636f6e7461696e65722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/gravatalonga/container/code-structure)[![Quality Score](https://camo.githubusercontent.com/0de6ed758f727f4c03e738bab9f552c8d926b2a11b044501ee1d7defceadacb2/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f677261766174616c6f6e67612f636f6e7461696e65722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/gravatalonga/container)[![Type coverage](https://camo.githubusercontent.com/4dd775ac2cebda09b5a662baf84e2caa15c325fa5d66700cbdc9258c8d5a503c/68747470733a2f2f73686570686572642e6465762f6769746875622f677261766174614c6f6e67612f636f6e7461696e65722f636f7665726167652e737667)](https://shepherd.dev/github/gravataLonga/container)[![PHP Unit](https://github.com/gravataLonga/container/workflows/Continuous%20Integration/badge.svg?branch=master)](https://github.com/gravataLonga/container/actions?query=workflow%3A%22Continuous+Integration%22++)[![Say Thanks](https://camo.githubusercontent.com/ee843c523d57170345478ba1c560741cf66c2e266144266ccd1ab0c51b7f87a5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5361792532305468616e6b732d212d3145414544422e737667)](https://saythanks.io/to/jonathan.alexey16@gmail.com)

Container implementation which follow PSR-11.

Requirements
------------

[](#requirements)

PHPVERSIONContainer Version8.0 &lt;&lt;= 1.61.0&gt;= 8.0&gt;= 1.71.0&gt;= 8.0&gt;= 1.82.0Installation
------------

[](#installation)

```
composer require gravatalonga/container
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

```
use Gravatalonga\Container\Container;

$container = new Container();
$container->set('random', function() {
    return rand(0, 1000);
});

$container->share('uniqueSeed', function() {
    return rand(0, 1000);
});

// create alias 'sessionId'
$container->alias('uniqueSeed', 'sessionId');

echo $container->get('random'); // get random number each time you call this.

if ($container->has('uniqueSeed'))  {
    echo $container->get('uniqueSeed'); // get same random number.
}
```

When creating a new instance of Container, you can pass on first argument configurations or entries to be already bonded into container.

```
use Gravatalonga\Container\Container;

new Container([
    'settings' => ['my-settings'],
    FooBar::class => function (\Psr\Container\ContainerInterface $c) {
        return new FooBar();
    }
]);
```

### Using Service Provider

[](#using-service-provider)

```
use Gravatalonga\Container\Container;

$container = new Container();
$container->set(RedisClass::class, function () {
    return new RedisClass();
});

// then you can use...
$cache = $container->get('Cache');
```

When using `set`, `factory` or `share` with Closure and if you want to get `Container` it's self, you can pass type hint of `ContainerInterface` as argument.

```
use Gravatalonga\Container\Container;
use Psr\Container\ContainerInterface;

$container = new Container([
    'server' => 'localhost',
    'username' => 'root'
]);

$container->set('Cache', function (ContainerInterface $container) {
    // some where you have binding this RedisClass into container...
    return $container->make(RedisClass::class, [
        'server' => $container->get('server'),
        'username' => $container->get('username')
    ]);
});

// set is a method alias of factory
$container->factory('CacheManager', function() {
    return new CacheManager();
});

// then you can use...
$cache = $container->get('Cache');
```

### Using Array like access

[](#using-array-like-access)

```
use Gravatalonga\Container\Container;

$container = new Container();
$container[FooBar::class] = function(ContainerInterface $container) {
    return new FooBar($container->get('settings'));
};

if (isset($container[FooBar::class])) {
    echo $container[FooBar::class]->helloWorld();
}
```

### Alias

[](#alias)

Alias like the name show, it to make a possibility to make an alias from one entry to another. It will throw an exception if can't be found.

```
use Gravatalonga\Container\Container;

$container = new Container();
$container->set('settings', ['driver' => 'default']);
$container->set(FooBar::class, function($settings) {
    return new FooBar($settings);
});

$container->alias(FooBar::class, 'foo.bar');

$foobar = $container->get('foo.bar');
```

### Callable as alternative

[](#callable-as-alternative)

```
use Gravatalonga\Container\Container;

$class = new class
{
    public function get(): int
    {
        return mt_rand(0, 100);
    }
};

$container = new Container();
$container->factory('random', [$class, 'get']);

$foobar = $container->get('random'); // it will get random int
```

### Extend

[](#extend)

In order implementation to be ease for other services providers `extend` method was created.

```
use Gravatalonga\Container\Container;

class Test
{
    /**
     * @var string
     */
    public $name;

    public function __construct($name = 'Jonathan Fontes')
    {
        $this->name = $name;
    }
}

$container = new Container();

$container->get(Test::class, function(ContainerInterface $container) {
    return new Test;
});

$container->extend(Test::class, function(ContainerInterface $container, $test) {
    return new Test($test->name.' - The greatest!');
});

echo $container->get(Test::class); // It will print 'Jonathan Fontes - The greatest!';
```

### Advance usage

[](#advance-usage)

Container is capable to resolve class who isn't bounded, it will resolve dependencies from `__construct` type-hint/built-in which is bounded. Read example code below:

> **Information**: built-in is type which is built in on PHP, which is `string`, `int`, `boolean`, etc. Type Hint is type which is created by user land, such as, when creating a class you are creating a new type.

### Using Type Hint Class

[](#using-type-hint-class)

```
use Gravatalonga\Container\Container;

class FooBar {}

class Test
{
    public function __construct(FooBar $foobar)
    {
        $this->foobar = $foobar;
    }
}

$container = new Container();
$container->set(FooBar::class, function () {
    return new FooBar();
});

$container->get(Test::class); // FooBar it will inject into Test class.
```

**Note:** We only support resolving auto wiring argument on construction if they are bounded into container. Otherwise it will throw an exception if can't find entry.

### Using Built in type

[](#using-built-in-type)

```
use Gravatalonga\Container\Container;
class Test
{
    public function __construct(string $name)
    {
        $this->name = $name;
    }
}

$container = new Container();
$container->set('name', 'my-var');

$container->get(Test::class); // my-var it will inject into Test class.
```

If argument accept nullable if can't be resolve it will pass default value which in this case is `null`.

```
use Gravatalonga\Container\Container;

class Test
{
    /**
     * @var string
     */
    private $name;

    public function __construct(string $name = null)
    {
        $this->name = $name;
    }
}

$container = new Container();

$container->get(Test::class); // null it will inject into Test class.
```

In also attempt to resolve auto wiring of construction by its default value, it will check default value of `__construct` and it will pass that default value.

First case, if value is a simple built-in type value.

```
use Gravatalonga\Container\Container;

class Test
{
    /**
     * @var string
     */
    private $name;

    public function __construct($name = 'Jonathan Fontes')
    {
        $this->name = $name;
    }
}

$container = new Container();

$container->get(Test::class); // 'Jonathan Fontes' it will pass into container...
```

### Tip

[](#tip)

It's well-known that using singleton pattern, it's an anti-pattern. But small feature can't hurt you

So, you can use:

```

$container = new Container();

// ...

Container::setInstance($container);

```

Then you can get instance of container,

```
$container = Container::getInstance();

```

Tips: The container can detected circular dependencies.

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
composer grumphp
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](.github/CODE_OF_CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email jonathan.alexey16\[at\]gmail.com instead of using the issue tracker.

Credits
-------

[](#credits)

- [Jonathan Fontes](https://github.com/gravatalonga)
- [Contributors](https://github.com/gravataLonga/container/graphs/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 90.9% 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.

###  Release Activity

Cadence

Every ~31 days

Recently: every ~71 days

Total

32

Last Release

1256d ago

PHP version history (4 changes)1.4.1PHP &gt;= 7.1.3

1.7.0PHP &gt;= 7.2

1.7.1PHP ^8.0

1.8.1PHP ^8.0|^8.1

### Community

Maintainers

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

---

Top Contributors

[![gravataLonga](https://avatars.githubusercontent.com/u/1126525?v=4)](https://github.com/gravataLonga "gravataLonga (90 commits)")[![drupol](https://avatars.githubusercontent.com/u/252042?v=4)](https://github.com/drupol "drupol (9 commits)")

---

Tags

containerinjection-containerphpphp71php72php73php74php80php81psr-11

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[pimple/pimple

Pimple, a simple Dependency Injection Container

2.7k130.5M1.4k](/packages/pimple-pimple)[league/container

A fast and intuitive dependency injection container.

86787.8M343](/packages/league-container)[lctrs/psalm-psr-container-plugin

Let Psalm understand better psr11 containers

17648.1k13](/packages/lctrs-psalm-psr-container-plugin)[phpwatch/simple-container

A fast and minimal PSR-11 compatible Dependency Injection Container with array-syntax and without auto-wiring

1810.1k2](/packages/phpwatch-simple-container)

PHPackages © 2026

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