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

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

substancephp/container
======================

A PSR-11 IoC container for PHP

v0.6.1(1y ago)11221MITPHPPHP &gt;=8.3CI passing

Since Jun 22Pushed 4mo agoCompare

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

READMEChangelog (10)Dependencies (4)Versions (13)Used By (1)

SubstancePHP: Container
=======================

[](#substancephp-container)

[![CI](https://github.com/substancephp/container/actions/workflows/ci.yml/badge.svg)](https://github.com/substancephp/container/actions/workflows/ci.yml/badge.svg)

Overview
--------

[](#overview)

`substancephp/container` is a dependency injection package for PHP. It offers:

- A container class that implements the [PSR-11 container interface](https://www.php-fig.org/psr/psr-11/)
- A container inheritance mechanism
- Automatic parameter injection into closures, using either type hinting or attributes
- Optional autowiring

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

[](#installation)

```
composer require substancephp/container

```

Usage
-----

[](#usage)

The following is an illustrative code passage showing how to use the `SubstancePHP\Container` class to define dependencies for injection.

```
use Laminas\Diactoros\ServerRequestFactory;
use Psr\Http\Message\ServerRequestInterface;
use SubstancePHP\Container\Container;
use SubstancePHP\Container\Inject;

// Initialize a `Container` instance from an array. The array's keys are strings
// (typically, but not necessarily, class, enum or interface names), and the array
// values are callbacks telling the container how to construct each given dependency.

$container = Container::from([

    // Example of simple "manual" definition:
    Foo::class => function (): Foo {
        return new Foo('example', 'constructor', 'parameters');
    },

    // Dependencies can also be literal values; and we can use arrow functions for
    // brevity:
    'ttl-seconds' => fn () => 30,

    // Example of manual definition by passing the container to the callback.
    // This can be used to get other dependencies required by the definition.
    BarInterface::class => function (Container $c): BarInterface {
        return new BarImplementation($c->get(Foo::class));
    },

    // Example of autowiring.
    // By referencing the `Container::autowire` method as a closure, we can
    // arrange for the construction of the given dependency using reflection
    // on its constructor parameters:
    Baz::class => Container::autowire(...),

    // The `Container::autowire` method can also be used within the callback
    // itself:
    BaqInterface::class => function (Container $c): BaqInterface {
        return Container::autowire($c, BaqImplementation::class);
    },
]);

// You can spawn a child container that efficiently inherits the parent container's
// definitions, and augments/overrides them with additional definitions. This might
// be done, for example, to initialize a separate `Container` instance per HTTP
// request, if your application's architecture requires that.
// Note that the *parent* container can be *any* instance of
// `Psr\Container\ContainerInterface`; it need not be an instance of
// `SubstancePHP\Container\Container`.
$container2 = Container::extend($container, [
    ServerRequestInterface::class => fn () => ServerRequestFactory::fromGlobals(),
]);

// A `Container` can be invoked to execute an arbitrary callable, autowiring its
// parameters with dependencies retrieved from the container:

$container2->run(function(

    // Parameters of named, non-scalar types will be injected based on the
    // class/interface/enum name:

    Foo $foo,

    ServerRequestInterface $request,

    // The `SubstancePHP\Container\Inject` attribute allows us to specify, by key, the
    // specific dependency to be injected for the given parameter. This is especially
    // useful when the parameter is of a scalar type:

    #[Inject('ttl-seconds')] int $ttl,

): void {
    // ... do stuff
});

// Similarly, if a class is autowired using Container::autowire(...), you can
// use the `Inject` attribute in its constructor, to specify dependencies for
// parameters that cannot be inferred automatically:

public function __construct(
    Bar $bar,
    #[Inject('thing.xyz')] int $someParam,
) {
}
```

Note, dependencies are always initialised lazily, when and only when the container is required to provide them.

Also, after a given dependency has been looked up the first time, it is cached internally within the container, and the same instance will be returned again the next time. This behaviour is notably different to that of, say, Laravel's service container, which returns a new instance by default on each lookup.

Performance
-----------

[](#performance)

`substancephp/container` aims to offer a very simple, yet flexible API surface, while conforming with the PSR-11 container interface.

It is not a goal of the library to have the highest possible runtime performance; but rather, to perform well enough for the vast majority of use cases.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance59

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

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

Total

12

Last Release

528d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b92046d7067a88a8fa81cd464784b3cf41d1e2cc691613fd574770e69747e8e1?d=identicon)[matt-harvey](/maintainers/matt-harvey)

---

Top Contributors

[![matt-harvey](https://avatars.githubusercontent.com/u/6045346?v=4)](https://github.com/matt-harvey "matt-harvey (33 commits)")

---

Tags

PSR-11dependency-injection

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k48.9M994](/packages/php-di-php-di)[laminas/laminas-servicemanager

Factory-Driven Dependency Injection Container

15955.1M694](/packages/laminas-laminas-servicemanager)[slince/di

A flexible dependency injection container

20260.4k6](/packages/slince-di)[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)
