PHPackages                             kris-ro/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. kris-ro/php-dependency-injection

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

kris-ro/php-dependency-injection
================================

Dependency injection container for php

v1.0.0(1y ago)210MITPHPPHP 8.\*

Since Sep 14Pushed 1y ago1 watchersCompare

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

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

PHP Dependency Injection Container
==================================

[](#php-dependency-injection-container)

Dependency injection container class for php. Extends `kris-ro/php-config` functionality and relies on it for configuration.

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

[](#installation)

Use composer to install *PHP Dependency Injection Container*.

```
composer require kris-ro/php-dependency-injection
```

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

[](#configuration)

The `Container` reads the definitoin for the requested service from the config array built by `Container::buildConfig()`.
All services must be defined in the `services` branch of the configuration array. Each service is identified by its service idendifier (`service_identifier` in the example below).
First key in the definition array must be the `class` containing the class name prefixed by the namespace and optionally sufixed with the method name. Valid examples:

```
{
  "services" : {
    "service_identifier" : {
      "class" : "\\Class\\With\\Namespace"
    },
    "another_service_identifier" : {
      "class" : "\\Class\\With\\Namespace::MethodName"
    }
  }
}
```

The `class` can be followed by other entries in the definition array representing the methods of the created service that will be executed (in the same order as they are listed in the definition) by the container before the service is delivered. Valid examples:

```
{
  "services" : {
    "myPDO" : {
      "class": "\\PDO",
      "_construct": {
        "dsn": "mysql:host=localhost;dbname=test",
        "username": "k",
        "password": "123456"
      }
    },
    "validator" : {
      "class": "\\KrisRo\\Validator\\Validator",
      "createRegexRules" : {
        "rules": {
          "alphanumeric": "/^[a-z0-9\\-_]+$/i"
        }
      }
    }
  }
}
```

As you can see above, for `myPDO` the `_construct` is specified because it needs those three arguments.
The second definition `validator` also specifies a method to be executed with one argument `rules`.

The *names of the arguments* in the definition are taken from the method.
If you look at the `PDO` definition above you'll see that `_constructor` has three arguments `dsn`, `username` and `password` that are maped to `PDO`'s constructor arguments `$pdo`, `$username` and `$password` respectively.

There are three *types of argument values*:

- a **service identifier** prefixed with the character `@`
- an **entry path** from the config array prefixed with the character `#`
- anything else is passed as is

**Service as argument**
Value of `credentialsOrPDO` references `myPDO` service.

```
{
  "services" : {
    "myPDO" : {
      "class": "\\PDO",
      "_construct": {
        "dsn": "mysql:host=localhost;dbname=test",
        "username": "k",
        "password": "123456"
      }
    },
    "model" : {
      "class": "\\KrisRo\\PhpDatabaseModel\\Model",
      "_construct": {
        "credentialsOrPDO": "@myPDO"
      }
    }
  }
}
```

**Entry path from config array**
Value of `rules` references `validator > rules` entry in configuration array.

```
{
  "validator" : {
    "rules": {
      "alphanumeric": "/^[a-z0-9\\-_]+$/i"
    }
  },
  "services" : {
    "validator": {
      "class": "\\KrisRo\\Validator\\Validator",
      "_construct": {
        "rules": "#validator/rules"
      }
    }
  }
}
```

Usage
-----

[](#usage)

Once the service is defined it is as simple as:

```
use KrisRo\PhpDependencyInjection\Container;

# first build the configuration array. See more at https://github.com/kris-ro/php-config
Container::buildConfig('/absolute/path/to/your/folder/with/json/files');

# load the service you need
$pdo = Container::service('myPDO');
$model = Container::service('model');
$validator = Container::service('validator');
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity39

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

602d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/37bd75c9e8017bb2ffd7fc58e0e9b9aea5f335df3d2d7ca84805fd6436a1ace4?d=identicon)[kris-ro](/maintainers/kris-ro)

---

Top Contributors

[![kris-ro](https://avatars.githubusercontent.com/u/56273996?v=4)](https://github.com/kris-ro "kris-ro (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[phmlabs/annovent

Simple lightweight event dispatcher using annotations for registering listeners

113.3k](/packages/phmlabs-annovent)

PHPackages © 2026

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