PHPackages                             mbretter/stk-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. mbretter/stk-di

ActiveLibrary

mbretter/stk-di
===============

Dependency injection made easy

3.2.0(10mo ago)03.2k↓100%BSD-3-ClausePHPPHP ^8CI failing

Since Jul 22Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/mbretter/stk-di)[ Packagist](https://packagist.org/packages/mbretter/stk-di)[ RSS](/packages/mbretter-stk-di/feed)WikiDiscussions master Synced today

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

stk-di
======

[](#stk-di)

[![License](https://camo.githubusercontent.com/2b599ab1ace97388d4375b87919f92781a1a61d016fe640a5c4d15fc365deed1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4253442d626c75652e737667)](https://opensource.org/licenses/BSD-3-Clause)[![PHP 8](https://camo.githubusercontent.com/1fb120b28c01242c7b808a19d88391780a96ce77b0e348ab4e8b51cbb585c394/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382d79656c6c6f772e737667)](http://www.php.net)[![Latest Stable Version](https://camo.githubusercontent.com/563b117b5f9f37fd7e8ad31b9eb46ec71e189614a23559333499d163269b36e8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d627265747465722f73746b2d64692e737667)](https://packagist.org/packages/mbretter/stk-di)[![Total Downloads](https://camo.githubusercontent.com/8c3209615f98b56c86b216f8db37b27baca523f2d5a665fc23630c38373677c0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d627265747465722f73746b2d64692e737667)](https://packagist.org/packages/mbretter/stk-di)[![CI](https://github.com/mbretter/stk-di/actions/workflows/ci.yml/badge.svg)](https://github.com/mbretter/stk-di/actions/workflows/ci.yml/badge.svg)

A simple dependency injection system usable with any container implementing the Psr\\Container\\ContainerInterface.

The service factory supports constructor injection and argument injection for services implementing the Injectable interface.

As a special feature OnDemand services are supported, they support service creation at runtime not at creation time, this avoids a dependency loading bloat for each request, due to complex dependencies. As a side effect OnDemand services may be used to inject objects which are not implementing the Injectable interface, like 3rd party services.

The library comes up with a dumb container implementation, this container is mainly used for testing and demonstration purposes.

Injectable
----------

[](#injectable)

The injectable interface is a dummy/empty interface for targeting injectable services. Services should implement this interface.

```
use Stk\Service\Injectable;

class MyService implements Injectable
{
...
}
```

Registering services
--------------------

[](#registering-services)

As a first step you have to put all your services into the container and it is recommended to put the service factory into the container too.

```
use Stk\Service\DumbContainer;
use Stk\Service\Factory;

class ServiceA implements Injectable
{

}

$container = new DumbContainer();
$container['config'] = [
    'param1' => 'foo',
    'param2' => 'bar'
];

$container['factory']  = new Factory($container); // put the service factory into the container
$container['serviceA'] = function (ContainerInterface $c) {
    return $c['factory']->get(ServiceA::class);
};
```

Argument injection
------------------

[](#argument-injection)

The service factory scans each service for private methods having injectables as argument and injects the service by fetching it from the container, using the argument name as key.

```
class ServiceA implements Injectable
{

}

class ServiceB implements Injectable
{
    protected $serviceA;

    // tell the factory to inject serviceA
    private function setServiceA(Injectable $serviceA)
    {
        $this->serviceA = $serviceA;
    }
}

$container['serviceA'] = function (ContainerInterface $c) {
    return $c['factory']->get(ServiceA::class);
};

$container['serviceB'] = function (ContainerInterface $c) {
    return $c['factory']->get(ServiceB::class);
};

$service = $this->container->get('serviceB');
```

Constructor injection
---------------------

[](#constructor-injection)

When instantiating services, the factory scans the constructor for argument names, if they are found in the container, the service is injected, default values are supported (if the container has no service with the given name). It is not needed, that the injected service implements the injectable interface, any container value may be injected.

```
class ServiceC implements Injectable
{
    protected $service;
    protected $whatever;

    public function __construct($serviceA, $whatever = [])
    {
        $this->service  = $serviceA;
        $this->whatever = $whatever;
    }
}
```

### Constructor injection with params

[](#constructor-injection-with-params)

If it is needed to pass some kind of static parameters (e.g. config settings) at declaration time and some additional parameters at instantiation time, the service factory skips the passed parameters and only injects the remaining arguments.

```
class ServiceK implements Injectable
{
    protected $config;
    public $param1;

    // $config should be passed at service declaration, $param1 at creation time
    public function __construct($config, $param1)
    {
        $this->config = $config;
        $this->param1 = $param1;
    }
}

// the container declaration for ServiceK, wrapped inside a Closure
$container['serviceK'] = function ($c) {
    return function ($param2) use ($c) {
        return $c['factory']->get(ServiceK::class, $c->get('config'), $param2);
    };
};

// accessing the service
/** @var Closure $serviceK */
$factory  = $container->get('serviceK');

/** @var ServiceK $serviceK */
$serviceK = $factory('val2');

$serviceK->param1 === 'val2';
```

OnDemand services
-----------------

[](#ondemand-services)

OnDemand services are wrappers around services to avoid the immediate instantiation of dependend services. In bigger projects with tons of services, it is very likely to run into a dependency bloat, ServiceA depends on ServiceB, ServiceB needs ServiceC ... at the end you have all your services instantiated, regardeless whether they are used or not.

```
class ServiceH implements Injectable
{
    public $arg1 = null;
    public $arg2 = null;

    public function __construct($arg1 = null, $arg2 = null)
    {
        $this->arg1 = $arg1;
        $this->arg2 = $arg2;
    }
}

class ServiceE implements Injectable
{
    /** @var OnDemand */
    public $onDemand;

    // trigger DI of OnDemand service H
    private function setService(OnDemand $serviceH)
    {
        $this->onDemand = $serviceH;
    }

    public function getService()
    {
        return $this->onDemand->getInstance();
    }

    public function newService()
    {
        return $this->onDemand->newInstance();
    }
}

$container['serviceE'] = function ($c) {
    return $c['factory']->get(ServiceE::class);
};
$container['onDemandServiceH'] = function ($c) {
    // the protect method wraps the service inside into the OnDemand injectable
    return $c['factory']->protect(ServiceH::class);
};

/** @var OnDemand $serviceH */
$serviceH = $container->get('serviceH');
$inst     = $serviceH->newInstance('foo', 'bar');

// or if you want to treat them as singleton
$inst     = $serviceH->getInstance('foo', 'bar');

/** @var ServiceE $serviceE */
$serviceE = $container->get('serviceE');

$svc = $serviceE->getService();
```

### Non injectables (3rd party services)

[](#non-injectables-3rd-party-services)

If you want to inject services not implementing the Injectable interface (http-clients, etc.), you can register them using the OnDemand service.

```
class ServiceJ implements Injectable
{
    /** @var OnDemand */
    protected $foreignService = null;

    private function setForeignServices(OnDemand $foreignService)
    {
        $this->foreignService = $foreignService;
    }

    public function getForeignService()
    {
        return $this->foreignService->getInstance();
    }
}

$container['foreignService'] = function ($c) {
    return $c['factory']->protect(stdClass::class);
};
$container['serviceJ'] = function ($c) {
    return $c['factory']->get(ServiceJ::class);
};

/** @var ServiceJ $serviceJ */
$serviceJ = $container->get('serviceJ');

$std = $serviceJ->getForeignService();
```

Reusability with traits
-----------------------

[](#reusability-with-traits)

Traits are very handy, if you do not want to duplicate the code when injecting the same service again and again.

Write a Trait with the property, geter and seter

```
use Stk\Service\OnDemand;

trait DependsOnServiceB
{
    /** @var OnDemand */
    protected $_serviceB;

    private function setServiceB(OnDemand $serviceB)
    {
        $this->_serviceB = $serviceB;

        return $this;
    }

    /**
     * @return ServiceB
     */
    protected function serviceB()
    {
        return $this->_serviceB->getInstance();
    }
}

...

class ServiceJ implements Injectable
{
    use DependsOnServiceB;
}
```

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance55

Moderate activity, may be stable

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

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

Recently: every ~385 days

Total

9

Last Release

300d ago

Major Versions

1.1.2 → 2.0.02021-03-09

2.0.1 → 3.0.02022-08-09

PHP version history (4 changes)1.0.0PHP ^7.2

2.0.0PHP ^7.4

2.0.1PHP ^7.4|^8.0

3.0.0PHP ^8

### Community

Maintainers

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

---

Top Contributors

[![mbretter](https://avatars.githubusercontent.com/u/1787808?v=4)](https://github.com/mbretter "mbretter (11 commits)")

---

Tags

dependencydiinjectable-interfaceinjectionphpservice-factorydependency-injectiondistk

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mbretter-stk-di/health.svg)

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

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k47.0M884](/packages/php-di-php-di)[laminas/laminas-servicemanager

Factory-Driven Dependency Injection Container

15954.0M657](/packages/laminas-laminas-servicemanager)[aura/di

A serializable dependency injection container with constructor and setter injection, interface and trait awareness, configuration inheritance, and much more.

352963.4k53](/packages/aura-di)[mrclay/props-dic

Props is a simple DI container that allows retrieving values via custom property and method names

3611.3M2](/packages/mrclay-props-dic)[slince/di

A flexible dependency injection container

20256.7k5](/packages/slince-di)[joomla/di

Joomla DI Package

15375.0k10](/packages/joomla-di)

PHPackages © 2026

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