PHPackages                             stubbles/ioc - 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. stubbles/ioc

ActiveLibrary[Framework](/categories/framework)

stubbles/ioc
============

Dependency injection.

v12.0.0(5mo ago)24.3k[1 PRs](https://github.com/stubbles/stubbles-ioc/pulls)4BSD-3-ClausePHPPHP ^8.3CI passing

Since Feb 21Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/stubbles/stubbles-ioc)[ Packagist](https://packagist.org/packages/stubbles/ioc)[ Docs](http://stubbles.net/)[ RSS](/packages/stubbles-ioc/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (65)Used By (4)

stubbles/ioc
============

[](#stubblesioc)

Dependency injection.

Build status
------------

[](#build-status)

[![Tests](https://github.com/stubbles/stubbles-ioc/workflows/Tests/badge.svg)](https://github.com/stubbles/stubbles-ioc/workflows/Tests/badge.svg)

[![Latest Stable Version](https://camo.githubusercontent.com/34d7895d7cb63b6e848569558098fa419ba4cfc1c40318ce3a7c1c864ef41d5f/68747470733a2f2f706f7365722e707567782e6f72672f73747562626c65732f696f632f76657273696f6e2e706e67)](https://packagist.org/packages/stubbles/ioc) [![Latest Unstable Version](https://camo.githubusercontent.com/28a28bd05d149dcb38286695aa01f330812bdd19679e521c5ce21f508fd0dfd2/68747470733a2f2f706f7365722e707567782e6f72672f73747562626c65732f696f632f762f756e737461626c652e706e67)](//packagist.org/packages/stubbles/ioc)

Installation
------------

[](#installation)

*stubbles/ioc* is distributed as [Composer](https://getcomposer.org/)package. To install it as a dependency of your package use the following command:

```
composer require "stubbles/ioc": "^12.0"

```

Requirements
------------

[](#requirements)

*stubbles/ioc* requires at least PHP 8.3.

Inversion of Control
--------------------

[](#inversion-of-control)

*stubbles/ioc* provides a very simple-to-use but still powerful [inversion of control container](http://martinfowler.com/articles/injection.html), which supports constructor and setter based dependency injection. The IoC container of *stubbles/ioc* is modeled after [Google Guice](http://code.google.com/p/google-guice/)and makes use of type hinting annotations. If you've never heard of type hinting or annotations, you should at first read the sections on these two topics:

- [Section on 'type hinting' in the PHP manual](http://www.php.net/language.oop5.typehinting)
- [Annotations](https://github.com/stubbles/stubbles-reflect#annotations) Section on annotations in the *stubbles/reflect*.

### The example code

[](#the-example-code)

Imagine, you are building a car configurator. To follow the rules of good design, you define interfaces for all components of a car and provide several classes that implement these components.

The interfaces in you application include:

```
interface Car {
    public function moveForward($miles);
}
interface Person {
    public function sayHello();
}
interface Tire {
    public function rotate();
}
interface Engine {
    public function start();
}
```

The implementations are:

```
class BMW implements Car {
    public function __construct(private Engine $engine, private Tire $tire, private Person $driver) {
    }
    public function moveForward($miles) {
        $this->driver->sayHello();
        $this->engine->start();
        $this->tire->rotate();
    }
}

class Schst implements Person {
    public function sayHello() {
        echo "My name is Stephan\n";
    }
}

class Goodyear implements Tire {
    public function rotate() {
        echo "Rotating Goodyear tire\n";
    }
}

class TwoLitresEngine implements Engine {
    public function start() {
        echo "Starting 2l engine\n";
    }
}
```

### Without the dependency injection framework

[](#without-the-dependency-injection-framework)

To create a new instance of an implementation of `Car` the following code is required:

```
    $tire   = new Goodyear();
    $engine = new TwoLitresEngine();
    $schst  = new Schst();

    $bmw    = new BMW($engine, $tire, $schst);
    $bmw->moveForward(50);
```

Creating objects manually like this has several drawbacks:

- Your application is bound to the concrete implementations instead of the interfaces
- Changing the implementation means changing existing code, which might break it
- The creation of objects is scattered throughout your application

Of course, real applications have a lot more classes, so things only get worse then.

### Enter 'Inversion of Control'

[](#enter-inversion-of-control)

*stubbles/ioc* tries to solve these problems by providing functionality to handle all dependency injections for you. This keeps your application clean of boilerplate code.

Furthermore, it allows you to centralize and/or modularize the definition of the concrete implementations for your interfaces or abstract types.

### A simple example

[](#a-simple-example)

To define the concrete implementations is done using an instance of `stubbles\ioc\Binder`:

```
$binder = new \stubbles\ioc\Binder();
$binder->bind('Car')->to('BMW');
$binder->bind('Tire')->to('Goodyear');
$binder->bind('Person')->to('Schst');
$binder->bind('Engine')->to('TwoLitresEngine');
```

In this short code snippet, you bound the interfaces from the example above to their concrete implementations.

If you now need an instance of the engine, you use the binder to create a `stubbles\ioc\Injector`, which can be used to create the desired `Engine`:

```
$injector = $binder->getInjector();
$engine = $injector->getInstance('Engine');
var_dump($engine);
```

This code snippet will now display:

```
object(TwoLitresEngine)#48 (0) {
}

```

As desired, it created an instance of the concrete implementation, that you bound to the interface.

Next, you probably want to get an instance of `Car` using the same approach:

```
    $injector = $binder->getInjector();
    $car = $injector->getInstance('Car');
    var_dump($car);
```

```
object(BMW)#33 (3) {
  ["driver:private"]=>
  NULL
  ["engine:private"]=>
  object(TwoLitresEngine)#37 (0) {
  }
  ["tire:private"]=>
  object(Goodyear)#40 (0) {
  }
}

```

*stubbles/ioc* created a new instance of `BMW`, as you bound it to `Car`, and as the constructor of `BMW` requires a `Tire` and an `Engine` instance, it created these instances as well. To determine the concrete classes to use, *stubbles/ioc*used the bindings you defined in the `stubbles\ioc\Binder` instance.

What you also can see is, that Stubbles did not inject an object into the `$driver` property, although you specified a binding for `Person`. *stubbles/ioc*will *never* inject any dependencies via setter methods.

Further features
----------------

[](#further-features)

- [Optional injection](docs/optional_injection.md)
- [Implicit bindings](docs/implicit_bindings.md)
- [Default implementations](docs/default_implementations.md)
- [Inject instances](docs/inject_instances.md)
- [Singletons](docs/singleton_scope.md)
- [Named parameters](docs/named_parameters.md)
- [Constant values](docs/constant_values.md)
- [List bindings](docs/list_bindings.md)
- [Map bindings](docs/map_bindings.md)
- [Closure bindings](docs/closure_bindings.md)
- [Injection providers](docs/injection_providers.md)
- [Create the whole application](docs/application.md)
- [Application properties](docs/application_properties.md)
- [Runtime environment](docs/runtime_environment.md)

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance82

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity92

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 72.1% 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 ~79 days

Recently: every ~544 days

Total

64

Last Release

160d ago

Major Versions

v7.1.1 → v8.0.02016-07-22

v8.0.1 → v9.0.02019-11-12

v9.0.0 → v10.0.02019-12-13

v10.2.0 → v11.0.02024-01-01

v11.0.0 → v12.0.02025-11-30

PHP version history (7 changes)2.0.0-beta1PHP &gt;=5.3.0

v3.5.0PHP &gt;=5.4.0

v7.0.0PHP &gt;=5.6.0

v8.0.0PHP ^7.0

v9.0.0PHP ^7.3

v11.0.0PHP ^8.2

v12.0.0PHP ^8.3

### Community

Maintainers

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

---

Top Contributors

[![mikey179](https://avatars.githubusercontent.com/u/190475?v=4)](https://github.com/mikey179 "mikey179 (62 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (24 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/stubbles-ioc/health.svg)

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

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M190](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M256](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M591](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

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