PHPackages                             tflori/dependency-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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. tflori/dependency-injector

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

tflori/dependency-injector
==========================

Easy and lightweight dependency injector implementing PSR-11

v2.3.0(4y ago)55.6k↓50%1[1 PRs](https://github.com/tflori/dependency-injector/pulls)2MITPHPPHP ^7.0 || ^8.0

Since Aug 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/tflori/dependency-injector)[ Packagist](https://packagist.org/packages/tflori/dependency-injector)[ RSS](/packages/tflori-dependency-injector/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (20)Used By (2)

Dependency Injector
===================

[](#dependency-injector)

[![.github/workflows/push.yml](https://github.com/tflori/dependency-injector/actions/workflows/push.yml/badge.svg)](https://github.com/tflori/dependency-injector/actions/workflows/push.yml)[![Test Coverage](https://camo.githubusercontent.com/f83b40cad2361e20a624884840c1c4a97c91bf66eb9f87156bdf53f2cc086cc0/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f33383139646563336132333461366432303665622f746573745f636f766572616765)](https://codeclimate.com/github/tflori/dependency-injector/test_coverage)[![Maintainability](https://camo.githubusercontent.com/239399c890ae3d252484ce4d715d319c18dcb92505c18e97f2d1895a4dac65f1/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f33383139646563336132333461366432303665622f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/tflori/dependency-injector/maintainability)[![Latest Stable Version](https://camo.githubusercontent.com/b08bd2b17920b5f0dff5c9e4b98a19710e3237b5e89a61e40296afe7cd234fc7/68747470733a2f2f706f7365722e707567782e6f72672f74666c6f72692f646570656e64656e63792d696e6a6563746f722f762f737461626c65)](https://packagist.org/packages/tflori/dependency-injector)[![Total Downloads](https://camo.githubusercontent.com/7a4e85a7a159a5a21ec340ae7dc578e076d5a00f3561dd9d5fa878b527f34f48/68747470733a2f2f706f7365722e707567782e6f72672f74666c6f72692f646570656e64656e63792d696e6a6563746f722f646f776e6c6f616473)](https://packagist.org/packages/tflori/dependency-injector)[![License](https://camo.githubusercontent.com/5415bd78b8aea024998dec997eca1e7a44fca74da07a16fd522d73440ef62a61/68747470733a2f2f706f7365722e707567782e6f72672f74666c6f72692f646570656e64656e63792d696e6a6563746f722f6c6963656e7365)](https://packagist.org/packages/tflori/dependency-injector)

A simple and lightweight dependency injector. Compatible to php standard recommendation for dependency injection containers ([PSR-11](https://www.php-fig.org/psr/psr-11/)).

What Is A Dependency
--------------------

[](#what-is-a-dependency)

Something that your application needs to work correct. For example an instance of `Calculator` or `Config` or an object that implements `CacheInterface`.

Basic Usage
-----------

[](#basic-usage)

One of the biggest problems in software development is the tight coupling of dependencies. Imagine a class that creates an instance from `DatabaseConnection` with `new DatabaseConnection()`. This is called a tight coupling - it is so tight that you would have to overwrite your autoloader (what is not always possible) to mock the database connection.

There are two solutions to test this class without the need to have a database connection during the tests.

### Passing The Dependency

[](#passing-the-dependency)

When creating an object of that class you can provide the `DatabaseConnection` to the constructor. For example `new MyService(new DatabaseConnection)`. In the tests you can then pass a mock `new MyService(m::mock(DatabaseConnection::class))`.

That would mean that everywhere in your application where you create an object of this class you have to know where your `DatabaseConnection` object is stored or create a new one. It's even more worse: when the interface for the class changes (for example an additional dependency get added) you will have to change this everywhere in your code.

Here comes the dependency injection into play. Here with the most strait forward and understandable way (a callback):

```
