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

ActiveLibrary

corviz/di-container
===================

A simple yet powerful container with auto wiring

v1.0(3y ago)28MITPHPPHP &gt;=7.4

Since Jun 28Pushed 3y ago1 watchersCompare

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

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

di-container
============

[](#di-container)

A simple yet powerful container with auto wiring

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

[](#installation)

```
composer require corviz/di-container

```

Usage
-----

[](#usage)

Here are some common scenarios you will meet when using container + dependency injection

For these examples, we will assume the following classes:

```
class A
{
    public funtion doSomething(){ /* ... */ }
    public function __construct() { echo "new instance of A"; }
}

class B
{
    public function __construct(A $a) { echo "new instance of B"; }
}
```

### Retrieve a new instance from container

[](#retrieve-a-new-instance-from-container)

Note that no previous declaration was required!

```
use Corviz\DI\Container;

$container = new Container();
$a = $container->get(A::class);

$a->doSomething(); //It works! :)
```

### Auto-wiring and parameter types

[](#auto-wiring-and-parameter-types)

What if we need a new instance of B?

```
$b = $container->get(B::class);

//Outputs:
//new instance of A
//new instance of B
```

Because we declared a parameter with type "A" in the constructor, and it is a class, our container will try to create a new instance automatically. This is done though Reflection.

If we needed to receive a primitive type as parameter, we have to declare a default value otherwise the container will throw a ContainerException, since it can't guess how to fill it.

### Manual setup

[](#manual-setup)

You may want to set some objects manually in some particular cases. To do so, simply use 'set' method.

```
//Whenever this interface is met, the container will declare a class instance instead:
$container->set(AInterface::class, A::class);

//Declaring a constructor. This function will be called whenever 'B' class is met.
//Note: a closure is REQUIRED, in this case
$container->set(B::class, function (){
    $param1 = new A();

    return new B($param);
});

//Defining an instance. Whenever this interface is met, the same object will be accessed
$container->set(AInterface::class, new A());
```

### Aliases

[](#aliases)

If we want to alias something, all we have to do is set it in our container as usual

```
$container->set('s3', new S3Client(/* ... */));

//...

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

### Singletons

[](#singletons)

When we want to access the same object through the entire execution, all we have to do is use 'setSingleton'. It accepts the same definition variants as 'set'. In other words, you may use the class name, a constructor closure or an object instance.

```
$container->setSingleton(Queue::class, function(){
    $queue = new Queue();

    //setup...

    return $queue;
});

$queue = $container->get(Queue::class);
$queue->add(/* ... */);
echo $queue->count(); //1

//Somewhere else:

$queue = $container->get(Queue::class);
$queue->add(/* ... */);
echo $queue->count(); //2
```

### Invoking methods

[](#invoking-methods)

Let's assume the following class:

```
class Person
{
    public function sayHello(string $name, string $surname = '')
    {
        echo "Hello $name $surname!";
    }

    public function sendMailMessage(PHPMailer $mail)
    {
        //send routine...
    }
}

$person = new Person();

// Inform only required parameters
$container->invoke($person, 'sayHello', [
    'name' => 'John'
]); //Hello John !

// Inform required and optional parameters
$container->invoke($person, 'sayHello', [
    'name' => 'John',
    'surname' => 'Doe',
]); //Hello John Doe!

// Mailer class automatically bound (auto wire)
$container->invoke($person, 'sendMailMessage');
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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

Unknown

Total

1

Last Release

1419d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5069326?v=4)[Carlos Alberto Bertholdo Carucce](/maintainers/carloscarucce)[@carloscarucce](https://github.com/carloscarucce)

---

Top Contributors

[![carloscarucce](https://avatars.githubusercontent.com/u/5069326?v=4)](https://github.com/carloscarucce "carloscarucce (2 commits)")

---

Tags

autowiringcontainerdependency-injectionphp

### Embed Badge

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

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