PHPackages                             liftkit/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. [Framework](/categories/framework)
4. /
5. liftkit/dependency-injection

ActiveLibrary[Framework](/categories/framework)

liftkit/dependency-injection
============================

Dependency injection library for LiftKit

v1.5.1(3y ago)32.0k4LGPL-2.1-onlyPHPCI failing

Since Feb 24Pushed 3y ago1 watchersCompare

[ Source](https://github.com/liftkit/dependency-injection)[ Packagist](https://packagist.org/packages/liftkit/dependency-injection)[ RSS](/packages/liftkit-dependency-injection/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (3)Versions (13)Used By (4)

Dependency Injection
====================

[](#dependency-injection)

A simple but featured dependency injection library, with automatic class-resolution.

Create a new container
----------------------

[](#create-a-new-container)

```
use LiftKit\DependencyInjection\Container\Container;

$container = new Container;
```

Rules
-----

[](#rules)

A rule is an anonymous function that defines how to create an object.

```
$container->setRule(
  'SomeRule',
  function ()
  {
    return new SomeClass();
  }
);

$someObject = $container->getObject('SomeRule');

// $someObject will be an instance of SomeClass
```

Singleton rules
---------------

[](#singleton-rules)

A rule will execute each time `getObject` is called by default. In order to force it to execute only once, we use `setSingletonRule`. Each call to `getObject` for the rule will return the same object.

```
$container->setSingletonRule(
  'SomeSingletonRule',
  function ()
  {
    return new SomeClass();
  }
);

$object1 = $container->getObject('SomeSingletonRule');
$object2 = $container->getObject('SomeSingletonRule');

// $object1 and $object2 are the same object
```

Rules with parameters
---------------------

[](#rules-with-parameters)

Some rules can have parameters passed to them. The first argument to the `setRule` callback is the container istelf. Each subsequent argument is supplied by an optional array of parameters supplied to `getObject`.

```
$container->setRule(
  'SomeRuleWithParameters',
  function (Container $container, $arg1, $arg2)
  {
    return new SomeClass($arg1, $arg2);
  }
);

$someObject = $container->getObject('SomeRuleWithParameters', ['arg1', 'arg2']);

// SomeClass will be contructed with 'arg1' and 'arg2' as the parameters to is constructor.
```

Rules that reference other rules
--------------------------------

[](#rules-that-reference-other-rules)

```
$container->setRule(
  'Rule1',
  function ()
  {
    return new SomeClass;
  }
);

$container->setRule(
  'Rule2',
  function (Container $container)
  {
    return new OtherClass($container->getObject('Rule1'));
  }
);

$someObject = $container->getObject('Rule2');

// $someObject will be an instance of OtherClass with a new instance of SomeClass injected as its
// first contructor argument.
```

Overriding rules
----------------

[](#overriding-rules)

Rules can be overridden by redefining them. This is useful for modular code.

```
$conatiner->setRule(
  'SomeRule',
  function ()
  {
    return new SomeClass;
  }
);

$container->setRule(
  'SomeRule',
  function ()
  {
    return new OtherClass;
  }
);

$someObject = $container->getObject('SomeRule');

// $someobject will be an instance of OtherClass
```

Storing an instance
-------------------

[](#storing-an-instance)

You can also store an object you've already created an bind it to a rule.

```
$someObject = new SomeObject;

$container->storeObject('SomeRule', $someObject);

$otherObject = $container->getObject('SomeRule');

// $someObject and $otherObject are the some object
```

Automatic resolution
--------------------

[](#automatic-resolution)

The container can also bind a rule to a class. An instance of a class can be created automatially by looking at the typehints of each constructor argument. In the case below, and instance of `B` is automatically created before creating an instance of `A`. The newly-created instance of B is then injected into the constructor of `A`.

```
class A
{
  private $b;

  public function __construct (B $b)
  {
    $this->b = $b;
  }

  public function getB ()
  {
    return $this->b;
  }
}

class B
{
  // placeholder class
}

$container->bindRuleToClass(
  'GiveMeANewA',
  A::class
);

$a = $container->getObject('GiveMeANewA');
$b = $a->getB();

// $a is a new instance of A. $b is a new instance of b.
```

Automatic resolution with rules
-------------------------------

[](#automatic-resolution-with-rules)

In some cases, you may need to tell the injector to create an instance following a different rule when it encounters the typehint of a certain class instead. In the example below, a rule is created for the construction of `B`. When the container realizes it needs an instance of `B` when creating an `A`, it will follow that rule to create `B` first. In this case, `A`'s constructor was injected with an instance of B created by the rule `'GiveMeANewB'` to create `$a`.

```
$container->setRule(
  'GiveMeANewB',
  function ()
  {
    $b = new B;

    $b->createdByRule = true;

    return $b;
  }
);

$container->bindClassToRule(
  B::class,
  'GiveMeANewB'
);

$a = $container->getObject('GiveMeANewA');
$b = $a->getB();

// $b->createdByRule is true
```

Automatic resolution with parameters
------------------------------------

[](#automatic-resolution-with-parameters)

Sometimes, there are additional parameters that need to be passed to the constructor of a new instance that is being automatically constructed. Below the variables `$param1` and `$param2` will be injectied into `C`'s constructor, while `B` will be created by the rule `'GiveMeANewB'` above. Any additional parameters must fall at the end of the constructor's list of parameters.

```
class C
{
  private $b;
  private $param1;
  private $param2;

  public function __construct (B $b, $param1, $param2)
  {
    $this->b      = $b;
    $this->param1 = $param1;
    $this->param2 = $param2;
  }

  public function getB ()
  {
    return $this->b;
  }

  public function getParam1 ()
  {
    return $this->param1;
  }

  public function getParam2 ()
  {
    return $this->param2;
  }
}

$container->bindRuleToClass(
  'GiveMeANewC',
  C::class
);

$param1 = 1;
$param2 = 2;

$c = $container->getObject(
  'GiveMeANewC',
  [
    $param1,
    $param2,
  ]
);

$b = $c->getB();
$cParam1 = $c->getParam1();
$cParam2 = $c->getParam2();

// $b is an instance of B
// $cParam1 is 1
// $cParam2 is 2
```

Binding classes to aliases
--------------------------

[](#binding-classes-to-aliases)

Sometimes, you may want the container to resolve to a subclass when it encounters a particular typehint. In the example below, a new instance of `D` will be injected into `A`, instead of an instance of `B`.

```
class D extends B
{
  // placeholder class
}

$container->bindClassToAlias(
  B::class,
  D::class
);

$a = $container->getObject('GiveMeANewA');
$d = $a->getB();

// $d is an instance of D
```

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity70

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

Total

12

Last Release

1206d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2d501887cec7d9f26e1b14f92b3759de77256f57e4dc00537507e94e25700560?d=identicon)[rwstream9](/maintainers/rwstream9)

---

Top Contributors

[![rwstream9](https://avatars.githubusercontent.com/u/1824185?v=4)](https://github.com/rwstream9 "rwstream9 (21 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/liftkit-dependency-injection/health.svg)

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

###  Alternatives

[laravel/framework

The Laravel Framework.

34.8k543.8M20.1k](/packages/laravel-framework)[symfony/symfony

The Symfony PHP framework

31.4k87.2M2.2k](/packages/symfony-symfony)[cakephp/cakephp

The CakePHP framework

8.9k19.5M1.8k](/packages/cakephp-cakephp)[bref/bref

Bref is a framework to write and deploy serverless PHP applications on AWS Lambda.

3.4k10.6M67](/packages/bref-bref)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[typo3/cms

TYPO3 CMS is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.

1.2k1.9M122](/packages/typo3-cms)

PHPackages © 2026

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