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

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

semperton/container
===================

Dynamic PSR-11 dependency injection container with reflection based autowiring.

3.0.0(1y ago)3105MITPHPPHP &gt;=8.0

Since Jan 21Pushed 1y ago1 watchersCompare

[ Source](https://github.com/semperton/container)[ Packagist](https://packagist.org/packages/semperton/container)[ RSS](/packages/semperton-container/feed)WikiDiscussions main Synced 6d ago

READMEChangelog (9)Dependencies (3)Versions (10)Used By (0)

[![Semperton](https://raw.githubusercontent.com/semperton/.github/main/readme-logo.svg)](https://github.com/semperton)Semperton Container
===================

[](#semperton-container)

A lightweight PSR-11 dependency injection container
with reflection based autowiring.

---

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

[](#installation)

Just use Composer:

```
composer require semperton/container

```

Container requires PHP 8.0+

Interface
---------

[](#interface)

```
new Container(iterable $definitions = [])
```

The container ships with four public methods:

```
withAutowiring(bool $flag): Container // toggle autowiring
withEntry(string $id, mixed $entry): Container // add a container entry
withDelegate(ContainerInterface $delegate): Container // register a delegate container
get(string $id): mixed // get entry (PSR-11)
has(string $id): bool // has entry (PSR-11)
create(string $id, array $params = []): mixed // create a class with optional constructor substitution args
entries(): array // list all container entries
```

Usage
-----

[](#usage)

Classes can be resolved automatically as long as they do not require any special configuration (autowiring).

```
use Semperton\Container\Container;

class World
{
	public function __toString()
	{
		return 'World';
	}
}

class Hello
{
	protected $world;
	public function __construct(World $world)
	{
		$this->world = $world;
	}
	public function print()
	{
		echo "Hello {$this->world}";
	}
}

$container = new Container();
$hello = $container->get(Hello::class);
$hello2 = $container->get(Hello::class);

$hello instanceof Hello::class // true
$hello === $hello2 // true
$hello->print(); // 'Hello World'
```

Note that the container only creates (shared) instances once. It does not work as a factory. You should consider the [Factory Pattern](https://designpatternsphp.readthedocs.io/en/latest/Creational/SimpleFactory/README.html) or use the `create()` method instead:

```
use Semperton\Container\Container;

class Mail
{
	public function __construct(Config $c, string $to)
	{
	}
}

class MailFactory
{
	public function createMail(string $to)
	{
		return new Mail(new Config(), $to);
	}
}

$mail1 = $container->get(MailFactory::class)->createMail('info@example.com');
$mail2 = $container->create(Mail::class, ['to' =>'info@example.com']);
```

The `create()` method will automatically resolve the `Config` dependency for `Mail`.

Configuration
-------------

[](#configuration)

You can configure the container with definitions. `Closures` are always treated as factories and should be used to bootstrap class instances. If you like to use `callables` as factories: `Closure::fromCallable([$object, 'method'])`.

```
use Semperton\Container\Container;

$container = new Container([

	'mail' => 'local@host.local',
	'closure' => static function () { // closures must be wrapped in another closure
		return static function () {
			return 42;
		};
	},
	MailFactory::class => new MailFactory('local@host.local'), // avoid this, instead do
	MailFactory::class => static function (Container $c) { // lazy instantiation with a factory

		$sender = $c->get('mail');
		return new MailFactory($sender);
	}, // or
	// factory params are automatically resolved from the container
	MailFactory::class => static fn (string $mail) => new MailFactory($mail),
	Service::class => static fn (Dependency $dep) => new Service($dep)
]);

$container->get('mail'); // 'local@host.local'
$container->get('closure')(); // 42
$container->get(MailFactory::class); // instance of MailFactory
```

The `withEntry()` method also treats `callables` as factories.

Immutability
------------

[](#immutability)

Once the container is created, it is immutable. If you like to add an entry after instantiation, keep in mind that the `withEntry()` method always returns a new container instance:

```
use Semperton\Container\Container;

$container1 = new Container();
$container2 = $container1->withEntry('number', 42);

$container1->has('number'); // false
$container2->has('number'); // true

$container1 === $container2 // false
```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance40

Moderate activity, may be stable

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

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

Recently: every ~322 days

Total

9

Last Release

500d ago

Major Versions

1.1.1 → 2.0.02021-06-23

2.3.0 → 3.0.02025-01-02

PHP version history (2 changes)2.0.0PHP &gt;=7.2.0

3.0.0PHP &gt;=8.0

### Community

Maintainers

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

---

Top Contributors

[![jrabausch](https://avatars.githubusercontent.com/u/38224080?v=4)](https://github.com/jrabausch "jrabausch (90 commits)")

---

Tags

autowiringcontainerdependency-injectionimmutabilityiocphppsr-11semperton

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/semperton-container/health.svg)](https://phpackages.com/packages/semperton-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.

86387.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)
