PHPackages                             ronanchilvers/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. ronanchilvers/container

ActiveLibrary

ronanchilvers/container
=======================

A very simple, PSR-11 compatible container

2.0(3y ago)02.1k1MITPHPPHP ^8.0CI failing

Since Mar 7Pushed 3y ago1 watchersCompare

[ Source](https://github.com/ronanchilvers/container)[ Packagist](https://packagist.org/packages/ronanchilvers/container)[ RSS](/packages/ronanchilvers-container/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (10)Dependencies (3)Versions (11)Used By (1)

container
=========

[](#container)

[![Build Status](https://camo.githubusercontent.com/182c24a9bf30f61edb3d24fff462023bf0f1345896f308ebd26428c455eb7a95/68747470733a2f2f7472617669732d63692e6f72672f726f6e616e6368696c766572732f636f6e7461696e65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ronanchilvers/container)[![codecov](https://camo.githubusercontent.com/ed9550b853cae516ed5c6bf7c55d85ea9ab4d16f868c2731eeea115fafa9fbe3/68747470733a2f2f636f6465636f762e696f2f67682f726f6e616e6368696c766572732f636f6e7461696e65722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/ronanchilvers/container)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

A simple, small [PSR-11](https://www.php-fig.org/psr/psr-11/) compliant container for PHP 7+. It has the following features:

- Factory and shared definitions
- Support for non object services (ie: storing key values)
- Aliases
- Autowiring

Installation
------------

[](#installation)

The easiest way to install is via composer:

```
composer install ronanchilvers/container

```

Usage
-----

[](#usage)

Basic usage is simple:

```
$container = new Container;
$container->set('my_service', function () {
    return new \My\Service();
});

$myService = $container->get('my_service');
```

By default services added to the container are factory services - you'll get a new one every time. If you want to define a shared service you can do:

```
$container = new Container;
$container->share('my_shared_service', function () {
    return new \My\Service();
});

$sharedService = $container->get('my_shared_service');
```

You can also register primitives with the container:

```
$container = new Container;
$container->set('settings', [
    'db' => [
        'adaptor'  => 'mysql',
        'username' => 'foobar',
        'password' => 'supersecret',
        'hostname' => '127.0.0.1'
    ]
]);
$container->set('my_string', 'foobar');

$settings = $container->get('settings');
$settings = $container->get('my_string');
```

### Aliases

[](#aliases)

Sometimes its useful to be able to alias a service. For example if you want to register a service with a simple string name but also refer to it by an interface name. To do this you can use a Symfony style prefix on the definition to indicate that its a reference to another service.

Here's an example:

```
$container = new Container;
$container->share('logger', function (){
    return new PSR11Logger();
});
$container->set('Psr\Log\LoggerInterface', '@logger');

// This:
$logger = $container->get('logger');
// returns the same instance as this:
$logger = $container->get('Psr\Log\LoggerInterface');
```

### Extending services

[](#extending-services)

The container allows services to be extended (just like Pimple) using the `extend()` method. You can extend both factory and shared services. Call `extend()` with the service id and a callable. The callable will recieve the service instance as its first argument and the container as the second. You can extend a service as many times as you like.

```
$container = new Container;
$container->share('my_service', function () {
    return new \My\Service;
});
$container->extend('my_service', function ($s, $c) {
    $s->registerWidget(new Widget);

    return $s;
});

$service = $container->get('my_service');
```

### Autowiring

[](#autowiring)

The container supports basic autowiring. This means that you can supply a fully qualified class name as a service definition and the container will attempt to instantiate it for you.

```
$container = new Container;
$container->set('logger', '\App\MyLogger');

$logger = $container->get('logger');
```

Constructor injection is also supported for type hinted parameters.

```
use Psr\Log\LoggerInterface;

class MyLogger implements LoggerInterface
{
    ...
}
class MyService
{
    public function __construct(LoggerInterface $logger)
    {
        ...
    }
}
$container = new Container;
$container->share(LoggerInterface::class, 'MyLogger');
$container->share(MyService::class, 'MyService');

// This will return an instantiated service with the logger injected
$service = $container->get(MyService::class);
```

The injected objects do not have to be registered with the container to be injected. If the container encounters a dependency that is not defined as a service it will attempt to create a new instance with no constructor parameters.

### Service Providers

[](#service-providers)

The container supports pimple style service providers. Your provider must implement `Ronanchilvers\Container\ServiceProviderInterface`.

```
class ServiceProvider implements ServiceProviderInterface
{
    /**
     * @author Ronan Chilvers
     */
    public function register(Container $container)
    {
        $container->set('my_service', function () {
            return new StdClass;
        });
    }
}

$container = new Container;
$container->register(new ServiceProvider);

$myService = $container->get('my_service');
```

Testing
-------

[](#testing)

The container is quite simple and has 100% test coverage. You can run the tests by doing:

```
./vendor/bin/phpunit

```

The default phpunit.xml.dist file creates coverage information in a build/coverage subdirectory.

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

[](#contributing)

If anyone has any patches they want to contribute I'd be more than happy to review them. Please raise a PR. You should:

- Follow PSR2
- Maintain 100% test coverage or give the reasons why you aren't
- Follow a one feature per pull request rule

License
-------

[](#license)

This software is licensed under the MIT license. Please see the [License File](LICENSE.md) for more information.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity74

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~191 days

Recently: every ~415 days

Total

10

Last Release

1269d ago

Major Versions

1.6 → 2.02022-11-25

PHP version history (2 changes)1.0PHP ^7.0

2.0PHP ^8.0

### Community

Maintainers

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

---

Top Contributors

[![ronanchilvers](https://avatars.githubusercontent.com/u/87890?v=4)](https://github.com/ronanchilvers "ronanchilvers (53 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[pimple/pimple

Pimple, a simple Dependency Injection Container

2.7k130.5M1.4k](/packages/pimple-pimple)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)[api-platform/state

API Platform state interfaces

223.4M57](/packages/api-platform-state)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)[symfony/json-streamer

Provides powerful methods to read/write data structures from/into JSON streams.

14440.0k8](/packages/symfony-json-streamer)[rubix/server

Deploy your Rubix ML models to production with scalable stand-alone inference servers.

632.3k](/packages/rubix-server)

PHPackages © 2026

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