PHPackages                             jankovacs/injector - 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. jankovacs/injector

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

jankovacs/injector
==================

Lightweight PHP dependency injector

v3.0.4(7y ago)111MITPHPPHP &gt;=7.2

Since Jul 26Pushed 7y ago2 watchersCompare

[ Source](https://github.com/jankovacs/injector)[ Packagist](https://packagist.org/packages/jankovacs/injector)[ RSS](/packages/jankovacs-injector/feed)WikiDiscussions master Synced 3d ago

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

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

[](#lightweight-php-dependency-injector)

[![Build Status](https://camo.githubusercontent.com/0be75222949a16a30b0ea8b1e3c4f6120553241224c9802114df1e78bc3fcdf0/68747470733a2f2f7472617669732d63692e636f6d2f6a616e6b6f766163732f696e6a6563746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/jankovacs/injector)

Injects the class dependencies based on constructor signature. Native types are not supported as dependencies (like int, array, string etc.) as into constructor one should pass just dependencies to other classes, not concrete data, e.g. 'locale' - that could be retrieved from a config class.

*Singleton support is kept, but it is not recommended to use singletons.*

Simple example for usage:
-------------------------

[](#simple-example-for-usage)

1. You have to specify somewhere in your application a config file for mappings, like this below:

```

class InjectionMappings {

    public function __construct(IInjector $injector)
    {
        $this->injector = $injector;
    }

    public function setMappings()
    {
        //------- will always create a new instance
        $this->injector->map( TestModel::class );

        //------- will always create a new instance of the mapped type
        $this->injector->map( ITestModel::class )->toType( TestModelOne::class );

        //------- will return with the mapped object
        $this->injector->map( TestModelOne::class )->toObject( new TestModelOne() );

        //------- will return with the singleton instance
        $this->injector->map( TestModelTwo::class )->asSingleton();

        //------- will return with the mapped class as singleton
        $this->injector->map( ITestSingleton::class )->toSingleton( TestSingleton::class );

        //------- example for unsing provider
        //-------
        //------- it is for having more complex mapping logic
        //------- you can map eg. an Interface to different end classes as different implementations
        //------- be carefull, a not proper use can lead to misbehavior of your application
        //-------
        //------- in this example the TestOne class will be injected to SomeController only
        //------- in all other classes, where the ITest is required, the TestTwo will be injected
        /** @var \injector\api\IInjectionProvider $providerMappings */
        $providerMappings = $this->injector->map( ITest::class )->toProvider( );
        $providerMappings->addUnique( 'SomeController' )->toSingleton( TestOne::class );
        //$providerMappings->addExceptTo( 'SomeController' )->toSingleton( TestTwo::class );
        $providerMappings->addToRest()->toSingleton( TestTwo::class );
    }
}

```

2. Then you need to instantiate the injector and the mapper classes and set the mappings ( setMappings() method in the example above, but it could be named to whatever you want )

```
// create injector
$injector = new \injector\impl\Injector();
// map you injection mapping class
$injector->map(InjectionMappings::class);
// get the intantiated mapper class from injector (it will be injected in this way)
$injectionMappings = $injector->getInstance(InjectionMappings::class);
// set the mappings
$injectionMappings->setMappings();

```

3. This is how you can inject dependencies into your classes

```
class IndexController
{
    public function __construct(ITestModel $testModel, ITestSingleton $testSingletion)
    {

    }
}

```

*You don't need to rewrite your code by adding some injector specific stuff (like in the initial version of this injector), it is injecting the needed dependencies by the constructor signature.*

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

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

Every ~6 days

Total

3

Last Release

2838d ago

Major Versions

v1.0.0 → v3.0.32018-08-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/568e3aab41ed71c6400b68d4d1f35af728086a9215c012143929ea0017be52a1?d=identicon)[jankovacs](/maintainers/jankovacs)

---

Top Contributors

[![janvkovacs](https://avatars.githubusercontent.com/u/196679785?v=4)](https://github.com/janvkovacs "janvkovacs (35 commits)")

###  Code Quality

TestsCodeception

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/jankovacs-injector/health.svg)

```
[![Health](https://phpackages.com/badges/jankovacs-injector/health.svg)](https://phpackages.com/packages/jankovacs-injector)
```

###  Alternatives

[liderman/php-text-generator

Fast SEO text generator on a mask

277.9k](/packages/liderman-php-text-generator)

PHPackages © 2026

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