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. [Utility &amp; Helpers](/categories/utility)
4. /
5. corviz/di-container

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

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

A simple yet powerful container with auto wiring

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

Since Jun 28Pushed 4y 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 today

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

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

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

1466d 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

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k455.6M9.6k](/packages/symfony-dependency-injection)[illuminate/contracts

The Illuminate Contracts package.

706130.3M13.3k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31182.0M2.4k](/packages/illuminate-container)[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k52](/packages/ecotone-ecotone)[symfony/type-info

Extracts PHP types information.

20069.8M270](/packages/symfony-type-info)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

751291.4k43](/packages/civicrm-civicrm-core)

PHPackages © 2026

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