PHPackages                             mcustiel/php-simple-di - 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. [Framework](/categories/framework)
4. /
5. mcustiel/php-simple-di

ActiveLibrary[Framework](/categories/framework)

mcustiel/php-simple-di
======================

Minimalist library to manage dependency injection with low memory usage and high performance.

1.2.1(11y ago)14577.4k↑115.3%22GPL-3.0+PHPPHP &gt;=5.5

Since Nov 30Pushed 7y ago2 watchersCompare

[ Source](https://github.com/mcustiel/php-simple-di)[ Packagist](https://packagist.org/packages/mcustiel/php-simple-di)[ RSS](/packages/mcustiel-php-simple-di/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (7)Dependencies (5)Versions (9)Used By (2)

php-simple-di
=============

[](#php-simple-di)

What is it?
-----------

[](#what-is-it)

php-simple-di (Php Simple Dependency Injection) is a library that provides a minimalistic dependency container with the ability to provide singleton or prototype versions of the dependencies identifying them by a name. php-simple-di provides a singleton class where you can register your dependencies, indentifying them by a name and then you can retrieve them. This library only creates instances on demand (it does not instanciate dependencies that are not needed for a request execution), so you only process and have in memory what you are using.

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

[](#installation)

### Composer:

[](#composer)

```
{
    "require": {
        // ...
        "mcustiel/php-simple-di": "^1.2.1"
    }
}
```

How to use it?
--------------

[](#how-to-use-it)

### Registering

[](#registering)

In your bootstrap file (or some startup script) you must define all the possible dependencies that your classes might need.

```
use Mcustiel\DependencyInjection\DependencyInjectionService;

$dependencyInjectionService = new DependencyInjectionService();
// ...
$dbConfig = loadDbConfig();
$cacheConfig = loadCacheConfig();
$dependencyInjectionService->register('dbConnection', function() use ($dbConfig) {
    return new DatabaseConnection($dbConfig);
});
$dependencyInjectionService->register('cache', function() use ($cacheConfig) {
    return new CacheManager($cacheConfig);
});
```

#### Getting dependencies

[](#getting-dependencies)

Then you can retrieve instances by refering them through their identifier.

```
$cacheManager = $dependencyInjectionService->get('cache');
```

### Instances

[](#instances)

php-simple-di creates "singleton" instances by default, this means everytime you request for a dependency it will return the same instance every time. If by any chance you need to change this behavior, you can define that every time you asks php-simple-di for a dependency it will return a new instance. This behavior is changed through a boolean parameter in **register** method.

#### Singleton behavior

[](#singleton-behavior)

```
$dependencyInjectionService->add('dbConnection', function() use ($dbConfig) {
    return new DatabaseConnection($dbConfig);
});
// or also you can make it explicit:
$dependencyInjectionService->register('cache', function() use ($cacheConfig) {
    return new CacheManager($cacheConfig);
}, true);

$instance1 = $dependencyInjectionService->get('cache');
$instance2 = $dependencyInjectionService->get('cache');
// $instance1 and $instance2 reference the same object
```

#### Prototype behavior

[](#prototype-behavior)

```
$dependencyInjectionService->register('dbConnection', function() use ($dbConfig) {
    return new DatabaseConnection($dbConfig);
}, false);

$instance1 = $dependencyInjectionService->get('cache');
$instance2 = $dependencyInjectionService->get('cache');
// $instance1 and $instance2 reference different objects
```

Notes
-----

[](#notes)

To simplify the previous examples I've shown config as previously obtained, but php-simple-di is perfectly capable of enclosing that functionality inside callbacks, even calling itself:

```
$dependencyInjectionService->add('config-loader', function() {
    return new SomeConfigLoaderService();
});

$dependencyInjectionService->add('config', function() {
    $injector = new DependencyInjectionService();
    $configLoader = $injector->get('config-loader');
    return $configLoader->load();
});

$dependencyInjectionService->add('dbConnection', function() {
    $injector = new DependencyInjectionService();
    $dbConfig = $injector->get('config');
    return new DatabaseConnection($dbConfig);
});
```

There's a lot of discussion around Singleton pattern, mentioning it as an antipattern because it's hard to test. Anyway, php-simple-di provides the container as a singleton class to allow just a single instance to be part of the execution. You should think in good practices and avoid using this class through singleton, but define it in your bootstrap file and pass the container instance as a parameter to your application dispatcher and always pass it as a parameter (injecting it as a dependency). Then, remember to use it properly, don't pass the container as a dependency, but use it to obtain the dependencies and pass them to your services.

```
// Do this:
$dbConnection = $dependencyInjectionService->get('dbConnection');
$personDao = new PersonDao($dbConnection); // Pass the proper dependency
// Instead of doing this:
$personDao = new PersonDao($dependencyInjectionService); // This works but is heretic and makes a little kitten cry.
```

###  Health Score

39

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity43

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity63

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

Total

6

Last Release

4186d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8d9b3ff93206038debfa1f16a11bbfc10fca9b2f4ddfdafa00e27365d290cf0d?d=identicon)[mcustiel](/maintainers/mcustiel)

---

Top Contributors

[![mcustiel](https://avatars.githubusercontent.com/u/3268370?v=4)](https://github.com/mcustiel "mcustiel (15 commits)")

---

Tags

dependency-injectionlibrarySimpleminimalist

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/mcustiel-php-simple-di/health.svg)

```
[![Health](https://phpackages.com/badges/mcustiel-php-simple-di/health.svg)](https://phpackages.com/packages/mcustiel-php-simple-di)
```

###  Alternatives

[goaop/framework

Framework for aspect-oriented programming in PHP.

1.7k4.1M33](/packages/goaop-framework)[jms/di-extra-bundle

Allows to configure dependency injection using annotations

32713.3M88](/packages/jms-di-extra-bundle)[nutgram/nutgram

The Telegram bot library that doesn't drive you nuts

729266.3k8](/packages/nutgram-nutgram)[yiisoft/injector

PSR-11 compatible injector. Executes a callable and makes an instances by injecting dependencies from a given DI container.

943.2M46](/packages/yiisoft-injector)

PHPackages © 2026

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