PHPackages                             pianissimo-php/dependency-injection - 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. pianissimo-php/dependency-injection

AbandonedArchivedLibrary

pianissimo-php/dependency-injection
===================================

The Dependency Injection Component allows you to implement the dependency injection design pattern.

110PHP

Since Aug 21Pushed 6y agoCompare

[ Source](https://github.com/pianissimo-php/dependency-injection)[ Packagist](https://packagist.org/packages/pianissimo-php/dependency-injection)[ RSS](/packages/pianissimo-php-dependency-injection/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

The Dependency Injection Component
==================================

[](#the-dependency-injection-component)

The Dependency Injection Component allows you to implement the dependency injection design pattern.

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

[](#installation)

```
composer require pianissimo-php/dependency-injection:master-dev

```

What is a service?
------------------

[](#what-is-a-service)

A service is a object which can be injected as an dependency of your class. The services are stored in the container.

Defining services
-----------------

[](#defining-services)

The most easy way to create a container is to use the `ContainerBuilder`. The `ContainerBuilder` implements the `ContainerInterface`.

```
$containerBuilder = new ContainerBuilder();
```

The `register()` method allows you to define a new service. It returns a `Definition` object. You can describe `MailerService`'s constructor by adding arguments using `addArgument()`

```
$containerBuilder
    ->register('mailer_service', MailerService::class)
    ->addArgument(new Reference('entity_manager_interface'))
    ->addArgument('SMTP');
```

You can also enable autowiring for the service definition:

```
$containerBuilder
    ->register('entity_manager', EntityManager::class)
    ->setAutowired(true);
```

You can also autowire services using the `autowire` method:

```
$containerBuilder->autowire(EntityManager::class);
```

The `Builder` class will autowire the `Definition` object.

You can also choose to autowire the services by default:

```
$containerBuilder->setDefaultAutowiring(true);
```

You can also define the service to be used for the injection of an interface like this:

```
$containerBuilder->add(EntityManagerInterface::class, new Reference('entity_manager'));
```

Add tags to your service definition using the `addTag()` method:

```
$containerBuilder
    ->register('dashboard_controller', DashboardController::class)
    ->addTag('controller')
    ->setAutowired(true);
```

After the container is built, you can use the `findServicesByTag()` method to retrieve the tagged service definitions (e.g. to use in a compiler pass).

```
$containerBuilder->findServicesByTag('controller');
```

Building the container
----------------------

[](#building-the-container)

Build the container using the `build` method:

```
$containerBuilder->build();
```

The Builder class builds all definitions and autowires all definitions of which the autowiring is enabled.

Compiler passes
---------------

[](#compiler-passes)

You can add compiler passes to the container, these are processed after all definitions have been built. Compiler passes must implement the `CompilerPassInterface`.

```
$containerBuilder->addCompilerPass($compilerPass);
```

Get a service from the container
--------------------------------

[](#get-a-service-from-the-container)

When the container is built, you can get a service from the container after it like this:

```
$mailerService = $containerBuilder->get('mailer_service');
```

The container loads the service when it is initialized, otherwise it will initialize the service using its built definition.

Avoid using the `get()` method, you should fetch your dependencies using the constructor. If you use the MVC pattern, you should define your controllers as services and use the `get()` method in your controller resolver to initialize the controller.

Injecting the container
-----------------------

[](#injecting-the-container)

The Dependency Injection Component does not allow you to inject the service container. Your services should not be container aware, they do not need to know how and that they have been injected. [Read more](https://stackoverflow.com/questions/10356497/is-is-an-anti-pattern-to-inject-di-container-to-almost-each-class)

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/2b1817da2d3b1b8aec540281e06eb766a9ee9f2655f92ff6538b213b3cee2bb2?d=identicon)[pianissimo-php](/maintainers/pianissimo-php)

---

Top Contributors

[![jonmldr](https://avatars.githubusercontent.com/u/33514542?v=4)](https://github.com/jonmldr "jonmldr (128 commits)")

### Embed Badge

![Health badge](/badges/pianissimo-php-dependency-injection/health.svg)

```
[![Health](https://phpackages.com/badges/pianissimo-php-dependency-injection/health.svg)](https://phpackages.com/packages/pianissimo-php-dependency-injection)
```

PHPackages © 2026

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