PHPackages                             greg-md/php-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. greg-md/php-dependency-injection

ActiveLibrary

greg-md/php-dependency-injection
================================

Dependency Injection technique for PHP.

0212[1 PRs](https://github.com/greg-md/php-dependency-injection/pulls)PHPCI failing

Since Jul 23Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/greg-md/php-dependency-injection)[ Packagist](https://packagist.org/packages/greg-md/php-dependency-injection)[ RSS](/packages/greg-md-php-dependency-injection/feed)WikiDiscussions master Synced 5d ago

READMEChangelogDependenciesVersions (2)Used By (0)

Greg PHP Dependency Injection
=============================

[](#greg-php-dependency-injection)

[![StyleCI](https://camo.githubusercontent.com/7c3702b48ab927d00a9a447eeb9693c3fc612345aa0acda732480483a2365608/68747470733a2f2f7374796c6563692e696f2f7265706f732f39353539313533362f736869656c643f7374796c653d666c6174)](https://styleci.io/repos/29315729)[![Build Status](https://camo.githubusercontent.com/22472f4a7735ae3aab2d1d066414794aab0156659ab5064130bcc70f901b6279/68747470733a2f2f7472617669732d63692e6f72672f677265672d6d642f7068702d646570656e64656e63792d696e6a656374696f6e2e737667)](https://travis-ci.org/greg-md/php-dependency-injection)[![Total Downloads](https://camo.githubusercontent.com/17fb66affc91e98b2dd387c3fe0f8e087b3ccda045e252e368a1d5cbe9c92084/68747470733a2f2f706f7365722e707567782e6f72672f677265672d6d642f7068702d646570656e64656e63792d696e6a656374696f6e2f642f746f74616c2e737667)](https://packagist.org/packages/greg-md/php-dependency-injection)[![Latest Stable Version](https://camo.githubusercontent.com/d37d5e50e5792eef5ca5dba761c5a126e4e05800ea5d4a0a7badcf27b9d98d39/68747470733a2f2f706f7365722e707567782e6f72672f677265672d6d642f7068702d646570656e64656e63792d696e6a656374696f6e2f762f737461626c652e737667)](https://packagist.org/packages/greg-md/php-dependency-injection)[![Latest Unstable Version](https://camo.githubusercontent.com/42d42f82a903c7fec206b8ad73ec3b41a9aae41df4d059b0041ce9b94d2d7a1f/68747470733a2f2f706f7365722e707567782e6f72672f677265672d6d642f7068702d646570656e64656e63792d696e6a656374696f6e2f762f756e737461626c652e737667)](https://packagist.org/packages/greg-md/php-dependency-injection)[![License](https://camo.githubusercontent.com/6b82957fd8ba6d3b54a371f6e28a3d65e4d0fa2439dc64b98e59f8fbfd61d00a/68747470733a2f2f706f7365722e707567782e6f72672f677265672d6d642f7068702d646570656e64656e63792d696e6a656374696f6e2f6c6963656e73652e737667)](https://packagist.org/packages/greg-md/php-dependency-injection)

Dependency Injection provides a lightweight, but powerful IoC Container that allows you to standardize and centralize the way objects are constructed in your application.

Table of Contents
=================

[](#table-of-contents)

- [Requirements](#requirements)
- [Installation](#installation)
- [How It Works](#how-it-works)
    - [Inject](#inject)
    - [Get](#get)
    - [Expect](#expect)
    - [Load](#load)
    - [Call](#call)
    - [Autoload](#autoload)
- [License](#license)
- [Huuuge Quote](#huuuge-quote)

Requirements
============

[](#requirements)

- PHP Version `^7.1`

Installation
============

[](#installation)

`composer require greg-md/php-dependency-injection`

How It Works
============

[](#how-it-works)

All you need to start using the [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection) technique, is to instantiate an IoC Container and inject objects in it.

```
$ioc = new \Greg\DependencyInjection\IoCContainer();
```

### Inject

[](#inject)

```
$ioc->inject('foo', Foo::class);

$ioc->inject('bar', new Bar());
```

**You can also inject in a more elegant way, using the object name as abstract.**

```
$ioc->register(new Foo());
```

The previous example is equivalent with:

```
$ioc->inject(Foo::class, new Foo());
```

**Customise the way your objects will be instantiated.**

```
$ioc->inject('redis.client', function() {
    $redis = new \Redis();

    $redis->connect();

    return $redis;
});
```

### Get

[](#get)

The next example will return null if the object is not injected in the IoC Container.

```
$foo = $ioc->get('foo');
```

### Expect

[](#expect)

The next example will throw an exception if parameter is not injected in the IoC Container.

```
$foo = $ioc->expect('foo');
```

### Load

[](#load)

In a real application to take advantage of what's best from Dependency Injection technique, you may want to instantiate objects with dependencies from the IoC Container without defining them manually. The best way to do that is to inject objects with it's names or it's strategies names as abstracts.

Let say we have the `Foo` class that requires a `BarStrategy` class.

```
class Foo
{
    private $bar;

    public function __construct(BarStrategy $bar)
    {
        $this->bar = $bar;
    }
}
```

What we do is inject the `BarStrategy` into the IoC Container and load the `Foo` class from it.

> `BarStrategy` is an `interface`, so, we don't break the [SOLID](https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)) principles.

```
$ioc->inject(BarStrategy::class, function() {
    return new Bar();
});

$foo = $ioc->load(Foo::class);
```

Sometimes you may want to redefine one or more dependencies of a class when loading it from the IoC Container.

```
class Foo
{
    private $bar;

    private $baz;

    public function __construct(BarStrategy $bar, BazStrategy $bar)
    {
        $this->bar = $bar;

        $this->baz = $baz;
    }
}
```

```
$ioc->inject(BarStrategy::class, function() {
    return new Bar();
});

$ioc->inject(BazStrategy::class, function() {
    return new Baz();
});
```

You can easily do it by defining those dependencies next after the class name in `load` method.

```
$customBaz = new CustomBaz();

$foo = $ioc->load(Foo::class, $customBaz);
```

The previous example will instantiate `BarStrategy` from the IoC Container, which is `Bar` class and for `BazStrategy` it will set the `CustomBaz` defined in the `load` method.

You can also load with arguments as array.

```
$ioc->loadArgs(Foo::class, [new CustomBaz()]);
```

### Call

[](#call)

You can call a callable with arguments injected in the Ioc Container the same way as [loading classes](#load).

```
$ioc->call(function(int $foo, Bar $bar) {
    // $bar will be injected from the Ioc Container.
}, 10);
```

You can also call a callable using arguments as array.

```
$ioc->callArgs([$someObj, 'someMethod'], ...$arguments);
```

### Autoload

[](#autoload)

You can autoload some classes by defining their prefixes/suffixes as abstracts.

```
$ioc->addPrefixes('Foo\\');

$ioc->addSuffixes('Controller');

$controller = $ioc->get(\Foo\BarController::class);
```

License
=======

[](#license)

MIT © [Grigorii Duca](http://greg.md)

Huuuge Quote
============

[](#huuuge-quote)

[![I fear not the man who has practiced 10,000 programming languages once, but I fear the man who has practiced one programming language 10,000 times. © #horrorsquad](https://camo.githubusercontent.com/73fc580403a8367b075fff0a0d7439dc0dc095dd3124b22757522675c1308df8/687474703a2f2f677265672e6d642f6875757567652d71756f74652d66622e6a7067)](https://camo.githubusercontent.com/73fc580403a8367b075fff0a0d7439dc0dc095dd3124b22757522675c1308df8/687474703a2f2f677265672e6d642f6875757567652d71756f74652d66622e6a7067)

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance53

Moderate activity, may be stable

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/85a6700372326d0546fcff3eb2c3f27daa711273bdd5d6fdda875a7d2f02af85?d=identicon)[greg-md](/maintainers/greg-md)

---

Top Contributors

[![greg-md](https://avatars.githubusercontent.com/u/10551984?v=4)](https://github.com/greg-md "greg-md (2 commits)")

---

Tags

dependency-injectiongreg-dependency-injectiongreg-mdgreg-phpphpphp-dependency-injectionweb-artisans

### Embed Badge

![Health badge](/badges/greg-md-php-dependency-injection/health.svg)

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

PHPackages © 2026

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