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

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

tobento/service-container
=========================

A PSR-11 container with autowiring.

2.0(7mo ago)144620MITPHPPHP &gt;=8.4

Since Jul 17Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-container)[ Packagist](https://packagist.org/packages/tobento/service-container)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-container/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (11)Used By (20)

Container Service
=================

[](#container-service)

The Container Service provides a PSR-11 container with autowiring.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
    - [Simple Example](#simple-example)
- [Documentation](#documentation)
    - [PSR-11](#psr-11)
    - [Autowiring](#autowiring)
    - [Definitions](#definitions)
    - [Make](#make)
    - [Call](#call)
    - [Resolver](#resolver)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the container service running this command.

```
composer require tobento/service-container

```

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

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design
- Autowiring

Documentation
=============

[](#documentation)

PSR-11
------

[](#psr-11)

```
use Tobento\Service\Container\Container;

$container = new Container();

$has = $container->has(Foo::class);

$foo = $container->get(Foo::class);
```

Autowiring
----------

[](#autowiring)

The container resolves any dependencies by autowiring, except build-in parameters needs a [definition](#definitions) to be resolved.

On union types parameter, the first resolvable parameter gets used if not set by definiton.

Definitions
-----------

[](#definitions)

**By providing the resolved object:**

```
use Tobento\Service\Container\Container;

class Foo
{
    public function __construct(
        private string $name
    ) {}
}

$container = new Container();

$container->set(Foo::class, new Foo('value'));

$foo = $container->get(Foo::class);
```

**By defining the missing parameters:**

```
use Tobento\Service\Container\Container;

class Foo
{
    public function __construct(
        private string $name
    ) {}
}

$container = new Container();

// By the construct method:
$container->set(Foo::class)->construct('value');

// By the with method using parameter name:
$container->set(Foo::class)->with(['name' => 'value']);

// By the with method using parameter position:
$container->set(Foo::class)->with([0 => 'value']);

$foo = $container->get(Foo::class);
```

**By using a closure:**

The container will automatically resolve any closure arguments.

```
use Tobento\Service\Container\Container;

class Foo
{
    public function __construct(
        private string $name
    ) {}
}

class Bar
{
    public function value(): string
    {
        return 'value';
    }
}

$container = new Container();

$container->set(Foo::class, static function(Bar $bar) {
    return new Foo($bar->value());
});

$foo = $container->get(Foo::class);
```

**You might configure which implementation to use:**

```
$container->set(BarInterface::class, Bar::class);
```

**Defining method calls:** You will need only to define build-in parameters as others get autowired if you want to.

```
use Tobento\Service\Container\Container;

class Foo
{
    public function index(Bar $bar, string $name) {}
}

class Bar {}

$container = new Container();

$container->set(Foo::class)->callMethod('index', ['name' => 'value']);

$container->set(Foo::class)->callMethod('index', [1 => 'value']);

$foo = $container->get(Foo::class);
```

**Prototype Definition:**

You might declare the defintion as prototype, meaning returning always a new instance.

```
use Tobento\Service\Container\Container;

class Foo {}
class Bar {}

$container = new Container();

$container->set(Foo::class)->prototype();

$container->set(Bar::class, function() {
    return new Bar();
})->prototype();

var_dump($container->get(Foo::class) === $container->get(Foo::class));
// bool(false)

var_dump($container->get(Bar::class) === $container->get(Bar::class));
// bool(false)
```

Make
----

[](#make)

The make() method works like get() except it will resolve the entry every time it is called.

```
use Tobento\Service\Container\Container;

class Foo
{
    public function __construct(
        private Bar $bar,
        private string $name
    ) {}
}

class Bar {}

$container = new Container();

$foo = $container->make(Foo::class, ['name' => 'value']);
```

Call
----

[](#call)

For more detail visit: [service-autowire#call](https://github.com/tobento-ch/service-autowire#call)

```
class Foo
{
    public function index(Bar $bar, string $name): string
    {
        return $name;
    }
}

class Bar {}

$container = new Container();

$name = $container->call([Foo::class, 'index'], ['name' => 'value']);
```

Resolver
--------

[](#resolver)

You might adjust your requirements by adding a custom resolver which implements the following interface:

```
use Tobento\Service\Container\Container;
use Tobento\Service\Container\ResolverInterface;

$container = new Container(new CustomResolver());
```

```
/**
 * ResolverInterface
 */
interface ResolverInterface
{
    /**
     * Resolve the given identifier to a value.
     *
     * @param string $id Identifier of the entry.
     * @param array $parameters
     *
     * @return mixed
     */
    public function resolve(string $id, array $parameters = []): mixed;

    /**
     * Resolve the given definition.
     *
     * @param DefinitionInterface $definition
     *
     * @return mixed The value of the resolved definition.
     */
    public function resolveDefinition(DefinitionInterface $definition): mixed;

    /**
     * If the given identifier is resolvable.
     *
     * @param mixed $id Identifier of the entry.
     * @return bool True if resolvable, otherwise false.
     */
    public function isResolvable(mixed $id): bool;
}
```

Credits
=======

[](#credits)

- [Tobias Strub](https://www.tobento.ch)
- [All Contributors](../../contributors)

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance62

Regular maintenance activity

Popularity18

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity75

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

Recently: every ~166 days

Total

11

Last Release

237d ago

Major Versions

1.x-dev → 2.02025-09-23

PHP version history (2 changes)1.0.1PHP &gt;=8.0

2.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/055d6a1b5c2384bb179c75ab0b55914231d898fdc4dffeb30770f81200e52206?d=identicon)[TOBENTOch](/maintainers/TOBENTOch)

---

Top Contributors

[![tobento-ch](https://avatars.githubusercontent.com/u/16684832?v=4)](https://github.com/tobento-ch "tobento-ch (18 commits)")

---

Tags

containerPSR-11Autowiringpackagetobento

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[php-di/php-di

The dependency injection container for humans

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

A flexible dependency injection container

20260.4k6](/packages/slince-di)[devanych/di-container

Simple implementation of a PSR-11 dependency injection container

124.2k3](/packages/devanych-di-container)[chubbyphp/chubbyphp-container

A simple PSR-11 container implementation.

1978.4k14](/packages/chubbyphp-chubbyphp-container)[selective/container

A simple PSR-11 container implementation with autowiring.

1220.4k4](/packages/selective-container)

PHPackages © 2026

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