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

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

fastero/php-di
==============

Simple and fast php dependency injector

v1.0.0(8y ago)018MITPHPPHP ^7.0

Since Dec 14Pushed 8y ago1 watchersCompare

[ Source](https://github.com/lehaBay/php-di)[ Packagist](https://packagist.org/packages/fastero/php-di)[ Docs](https://github.com/lehaBay/php-di)[ RSS](/packages/fastero-php-di/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

PHP Dependency Injector
=======================

[](#php-dependency-injector)

**Simple** and **fast** Dependency Injector. It supports autowire and autoloading.

requires php 7.0 +

Easy to install with **composer**
=================================

[](#easy-to-install-with-composer)

```
$ composer require fastero/php-di
```

Usage
=====

[](#usage)

```
$di = new \Fastero\DependencyInjector\DependencyInjector();

$user = $di->get(User::class);
```

As simple as this, no configuration needed just proper typehins for the constructor.

Of course by very definition of **dependency injector** you don't usually call $di-&gt;get() often, only in a very core of the application and then let all the dependencies to be injected.

More complete examples: let's create some classes to work with

```
class Company{

}
class User{
    public $company;
    public $language;
    //typehint Company says that first parameter expected to be of
    //type Company and DependencyInjetor can understand this
    public function __construct(Company $userCompany, $language = null)
    {
        $this->company = $userCompany;
        $this->language = $language;
    }
}

class UserFactory{
    public static function create(){
        return new User(new Company(), 'en');
    }
}
```

No configuration needed by default, but if you must here is how to use utility class **ServiceConfiguration**to add some configuration:

```
$configuration = [
        "services" =>[
            Company::class => ServiceConfiguration::setupClass(Company::class) //service that will create object of Company
                ->get(),//return complete configuration, should be very last call for every definition
            User::class => ServiceConfiguration::setupClass(User::class) //service that will create object of User
                ->addParameterService(Company::class) //first parameter in the constructor will service with name Company::class
                ->addParameterValue('fr') //second parameter will be value "fr"
                ->get(),//return complete configuration
            'forty-two' => ServiceConfiguration::setupClosure(function ($di, $serviceName){//closure will be called and returned value is a service
                        return 42;
                    })
                ->get(),
        ]
    ];
```

Using **ServiceConfiguration** is recommended way of creating configuration for services even though actual configuration is a simple array and can be created manually but this utility class helps to avoid mistakes. Using this class does not create bunch of objects so it has almost no overhead. Downside is that one have to call -&gt;get() at the very end of each definition

```
$configuration = [
        "services" =>[
            Company::class => ServiceConfiguration::setupClass(Company::class) //service that will create object of Company
                ->get(),//return complete configuration, should be very last call for every definition
            User::class => ServiceConfiguration::setupClass(User::class) //service that will create object of User
                ->addParameterService(Company::class) //first parameter in the constructor will service with name Company::class
                ->addParameterValue('fr') //second parameter will be value "fr"
                ->get(),//return complete configuration
            'forty-two' => ServiceConfiguration::setupClosure(function ($di, $serviceName){//closure will be called and returned value is a service
                        return 42;
                    })
                ->get(),
        ]
    ];

```

So creating configuration must be started with one of the **-&gt;setup\*(..)** methods and finished with **-&gt;get()** method which actually returns configuration array

```
$di = new Fastero\DependencyInjector\DependencyInjector($configuration);

//if you need to define some service after configuration is set there is a way
//here we create service type factory but sure thing it can be any supported type
$UserEngServiceConfiguration =
    ServiceConfiguration::setupFactory([UserFactory::class, 'create'])// call_user_function_array([UserFactory::class, 'create'], $params) will be called and return value is a service
        ->get();

$di->setServiceConfiguration('UserEng', $UserEngServiceConfiguration);
$service = $di->get(User::class );

var_dump($service instanceof User);
var_dump($service->language);

$service = $di->get("UserEng" );
var_dump($service instanceof User);
var_dump($service->language);

$service = $di->get("forty-two");
var_dump($service);
```

result:

```
bool(true)
string(2) "fr"
bool(true)
string(2) "en"
int(42)

```

MIT Licensed,

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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

3074d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e3097c5fa3377a0abdcd8e9bd3381698e19a1490ee562f98d3cd91b4727aa972?d=identicon)[fominleha](/maintainers/fominleha)

---

Top Contributors

[![lehaBay](https://avatars.githubusercontent.com/u/6716125?v=4)](https://github.com/lehaBay "lehaBay (13 commits)")

---

Tags

dependencyinjector

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[composer/composer

Composer helps you declare, manage and install dependencies of PHP projects. It ensures you have the right stack everywhere.

29.4k187.2M2.6k](/packages/composer-composer)[bamarni/composer-bin-plugin

No conflicts for your bin dependencies

52722.0M859](/packages/bamarni-composer-bin-plugin)[php-di/invoker

Generic and extensible callable invoker

26857.8M56](/packages/php-di-invoker)[dephpend/dephpend

Dependency analysis for PHP

534101.9k2](/packages/dephpend-dephpend)[contributte/di

Extra contrib to nette/di

465.8M18](/packages/contributte-di)[digital-creative/conditional-container

Provides an easy way to conditionally show and hide fields in your Nova resources.

116593.8k4](/packages/digital-creative-conditional-container)

PHPackages © 2026

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