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

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

gdetassigny/simple-container
============================

A simple implementation of the standard container interface, with support for autowiring

1.0(5y ago)023MITPHPPHP &gt;= 7.2

Since Oct 8Pushed 5y ago1 watchersCompare

[ Source](https://github.com/gabriel-detassigny/simple-container)[ Packagist](https://packagist.org/packages/gdetassigny/simple-container)[ RSS](/packages/gdetassigny-simple-container/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

Simple Container
================

[](#simple-container)

[![Build Status](https://camo.githubusercontent.com/f1c3baeb10b9ec777d3dfd5f370bc9c5e8209f921c388c0be80423cc1422ad2d/68747470733a2f2f7472617669732d63692e636f6d2f6761627269656c2d64657461737369676e792f73696d706c652d636f6e7461696e65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/gabriel-detassigny/simple-container)[![Coverage Status](https://camo.githubusercontent.com/f11ffdceed922427f386c44d4c2493a7a706a679e8d1ced7dd9c1fd855a7764a/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6761627269656c2d64657461737369676e792f73696d706c652d636f6e7461696e65722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/gabriel-detassigny/simple-container?branch=master)

This is a simple implementation of the [standard container interface](https://www.php-fig.org/psr/psr-11/), with support for autowiring.

This means that this package will attempt to figure out all your basic dependencies for you. It also supports manually defining dependencies in a YAML file, or even adding service providers for more complex dependencies.

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

[](#installation)

This package requires at least PHP 7.2.

Install it using composer:

```
composer require gdetassigny/simple-container

```

How to use it
-------------

[](#how-to-use-it)

### Basic usage

[](#basic-usage)

Setting up a basic container is quite simple:

```
use GabrielDeTassigny\SimpleContainer\ContainerProvider;

$container = (new ContainerProvider())->getContainer();
```

That's it! You can now request your dependencies from the container, thanks to autowiring.

```
$container->get(Foo\Bar::class); // returns an instance of Foo\Bar
```

### Autowiring

[](#autowiring)

The container will be able to instantiate simple services which have straightforward dependencies in their constructors:

The below examples can all be autowired:

```
$container = (new ContainerProvider())->getContainer();

class NoConstructor {}

$container->get(NoConstructor::class);

class ConstructorNoParam {
    public function __construct() {}
}

$container->get(ConstructorNoParam::class);

class ConstructorWithClassParam {
    public function __construct(stdClass $param) {}
}

// Note: $param will be also instantiate and injected in the constructor
$container->get(ConstructorWithClassParam::class);

class ConstructorWithDefaultParam {
    public function __construct(?string $test = null) {}
}

// Note: parameters with default values will be set to these values when autowired
$container->get(ConstructorWithDefaultParam::class);
```

Some cases cannot be autowired:

```
$container = (new ContainerProvider())->getContainer();

class ConstructorWithPrimitiveParam {
    public function __construct(string $test) {}
}

// Fails! Autowiring cannot figure out the value of the $test parameter.
// Consider using a service provider
$container->get(ConstructorWithPrimitiveParam::class);

interface FooInterface {}
class Bar implements FooInterface {}

// Fails! Autowiring does not know which concrete class you want
// Consider using either a YAML Config or a service provider
$container->get(FooInterface::class);
```

### YAML Config

[](#yaml-config)

Simple container also supports adding a YAML config to manually define dependencies.

This can be useful to define a concrete class implementation of an interface. Or you may want to define a different name for your service than its class name.

```
dependencies:
  Foo\Bar:
    name: Foo\BarInterface

  Foo\Baz:
    name: some-id
    dependencies:
      - Foo\BarInterface
```

Simply pass the path of the YAML file to the container provider:

```
use GabrielDeTassigny\SimpleContainer\ContainerProvider;

$container = (new ContainerProvider('/path/to/config.yaml'))->getContainer();

$container->get(Foo\BarInterface::class); // returns an instance of Foo\Bar
$container->get('some-id'); // returns an instance of Foo\Baz
```

### Service Provider

[](#service-provider)

If a service requires a more complex setup, you may want to look at using a service provider.

```
use GabrielDeTassigny\SimpleContainer\ServiceProvider;
use GabrielDeTassigny\SimpleContainer\ContainerProvider;

class FooServiceProvider implements ServiceProvider
{
    public function getService(): object
    {
        return new Foo('some-string');
    }
}

$containerProvider = new ContainerProvider();
$containerProvider->registerService(Foo::class, new FooServiceProvider());

$container = $containerProvider->getContainer();

$container->get(Foo::class); // returns the instance of Foo defined in FooServiceProvider
```

### Ordering

[](#ordering)

When called, the container will look for your service using all 3 above options in that order:

- service provider
- YAML config
- autowiring

Why use this instead of another container package?
--------------------------------------------------

[](#why-use-this-instead-of-another-container-package)

To be honest? Maybe you shouldn't. I built this mostly out of interest to understand how containers work under the hood. There are a lot of more evolved PHP containers out there.

However, I tried to make this package as straightforward to use as possible. If having a container up and running very quickly appeals to you then feel free to give this a go!

Furthermore, as it respects the standard container interface, you can always try it for a bit and later switch to another container package without too much hassle.

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 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 ~154 days

Total

2

Last Release

1938d ago

Major Versions

v0.1 → 1.02021-03-12

### Community

Maintainers

![](https://www.gravatar.com/avatar/870f4865ab3fedcba970923780adf4f1b2a6aeb9e3bb9ba82d7f5120b167a5fa?d=identicon)[gdetassigny](/maintainers/gdetassigny)

---

Top Contributors

[![gabriel-detassigny](https://avatars.githubusercontent.com/u/2980676?v=4)](https://github.com/gabriel-detassigny "gabriel-detassigny (22 commits)")

---

Tags

containerdependencydi

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[league/container

A fast and intuitive dependency injection container.

86894.4M437](/packages/league-container)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[capsule/di

A PSR-11 compliant autowiring dependency injection container.

2860.1k2](/packages/capsule-di)[aeliot/todo-registrar

Register TODOs from source code in issue tracker

153.0k](/packages/aeliot-todo-registrar)[miladrahimi/phpcontainer

Dependency injection (IoC) container for PHP projects

1324.1k2](/packages/miladrahimi-phpcontainer)

PHPackages © 2026

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