PHPackages                             xp-forge/inject - 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. xp-forge/inject

ActiveLibrary[Framework](/categories/framework)

xp-forge/inject
===============

Dependency injection for the XP Framework

v6.0.0(2y ago)045.0k↓18.8%[1 issues](https://github.com/xp-forge/inject/issues)[1 PRs](https://github.com/xp-forge/inject/pulls)BSD-3-ClausePHPPHP &gt;=7.4.0

Since Jan 10Pushed 1y ago2 watchersCompare

[ Source](https://github.com/xp-forge/inject)[ Packagist](https://packagist.org/packages/xp-forge/inject)[ Docs](http://xp-framework.net/)[ RSS](/packages/xp-forge-inject/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (36)Used By (0)

Inject
======

[](#inject)

[![Build status on GitHub](https://github.com/xp-forge/inject/workflows/Tests/badge.svg)](https://github.com/xp-forge/inject/actions)[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/)[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/9a6172e084c94723152bcb572529feef12ffa90abb84bb03908a917504f66b5f/68747470733a2f2f706f7365722e707567782e6f72672f78702d666f7267652f696e6a6563742f76657273696f6e2e737667)](https://packagist.org/packages/xp-forge/inject)

The inject package contains the XP framework's dependency injection API. Its entry point class is the "Injector".

Binding
-------

[](#binding)

Values can be bound to the injector by using its `bind()` method. It accepts the type to bind to, an optional name and these different scenarios:

- **Binding a class**: The typical usecase, where we bind an interface to its concrete implementation.
- **Binding an instance**: By binding a type to an existing instance, we can create a *singleton* model.
- **Binding a provider**: If we need more complicated code to create an instance, we can bind to a provider.
- **Binding a named lookup**: If we want control over the binding lookups for a type, we can bind to a `Named` instance.

```
use inject\{Injector, Bindings};
use com\example\{Report, HtmlReport, Storage, InFileSystem};

// Manually
$injector= new Injector(Bindings::using()
  ->typed(Report::class, HtmlReport::class)
  ->singleton(Storage::class, new InFileSystem('.'))
  ->named('title', 'Report title')
);

// Reusable via Bindings instances
class ApplicationDefaults extends Bindings {

  public function configure($injector) {
    $injector->bind(Report::class, HtmlReport::class);
    $injector->bind(Storage::class, new InFileSystem('.'));
    $injector->bind('string', 'Report title', 'title');
  }
}

$injector= new Injector(new ApplicationDefaults());
```

Instance creation
-----------------

[](#instance-creation)

Keep in mind: **"injector.get() is the new 'new'"**. To create objects and perform injection, use the Injector's get() method instead of using the `new` keyword or factories.

```
use inject\Injector;

$injector->bind(Report::class, HtmlReport::class);

// Explicit binding: Lookup finds binding to HtmlReport, creates instance.
$instance= $injector->get(Report::class);

// Implicit binding: No previous binding, TextReport instantiable, thus created.
$instance= $injector->get(TextReport::class);
```

Manual calls are usually not necessary though, instead you'll use injection:

Injection
---------

[](#injection)

Injection is performed by looking at a type's constructor. Bound values will be passed according to the given type hint.

```
class ReportImpl implements Report {

  public function __construct(ReportWriter $writer) { ... }
}
```

You can supply the type by using parameter attributes in case where the PHP type system is not concise enough. If the bound value's name differs from the parameter name, you can supply a name argument.

```
use inject\Inject;

class ReportImpl implements Report {

  public function __construct(
    ReportWriter $writer,
    Format $format,
    #[Inject(type: 'string[]', name: 'report-titles')]
    $titles
  ) { ... }
}
```

When a required parameter is encountered and there is no bound value for this parameter, an `inject.ProvisionException` is raised.

```
class ReportWriter implements Writer {

  public function __construct(Storage $storage) { ... }
}

$injector= new Injector();
$report= $injector->get(ReportWriter::class);  // *** Storage not bound
```

Method and field injection are not supported.

Configuration
-------------

[](#configuration)

As seen above, bindings can be used instead of manually performing the wiring. You might want to configure some of your app's settings externally instead of harcoding them. Use the `inject.ConfiguredBindings` class for this:

```
$injector= new Injector(
  new ApplicationDefaults(),
  new ConfiguredBindings(new Properties('etc/app.ini'))
);
```

The syntax for these INI files is simple:

```
web.session.Sessions=web.session.InFileSystem("/tmp")
name="Application"
```

Providers
---------

[](#providers)

Providers allow implementing lazy-loading semantics. Every type bound to the injector can also be retrieved by a provider. Invoking its get() method will instantiate it.

```
$provider= $injector->get('inject.Provider');

// ...later on
$instance= $provider->get();
```

Named lookups
-------------

[](#named-lookups)

If we need control over the lookup, we can bind instances of `Named`:

```
use inject\{Injector, Named, InstanceBinding};
use com\example\Value;

$inject= new Injector();
$inject->bind(Value::class, new class() extends Named {
  public function provides($name) { return true; }
  public function binding($name) { return new InstanceBinding(new Value($name)); }
});

$value= $inject->get(Value::class, 'default');  // new Value("default")
```

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity75

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

Recently: every ~187 days

Total

31

Last Release

785d ago

Major Versions

v1.1.0 → v2.0.02016-05-01

v2.2.0 → v3.0.02016-08-14

v3.1.0 → v4.0.02017-06-16

v4.4.0 → v5.0.02020-04-10

v5.5.0 → v6.0.02024-03-24

PHP version history (5 changes)v0.4.0PHP &gt;=5.4.0

v0.7.0PHP &gt;=5.5.0

v3.0.0PHP &gt;=5.6.0

v5.0.0PHP &gt;=7.0.0

v6.0.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/07d18d882c8b4aaf3466432f64018214f2771eda333202175431ee7233795376?d=identicon)[thekid](/maintainers/thekid)

---

Top Contributors

[![thekid](https://avatars.githubusercontent.com/u/696742?v=4)](https://github.com/thekid "thekid (266 commits)")

---

Tags

annotationsdependency-injectioninjectionphpxp-frameworkmodulexp

### Embed Badge

![Health badge](/badges/xp-forge-inject/health.svg)

```
[![Health](https://phpackages.com/badges/xp-forge-inject/health.svg)](https://phpackages.com/packages/xp-forge-inject)
```

###  Alternatives

[nwidart/laravel-modules

Laravel Module management

6.1k14.6M274](/packages/nwidart-laravel-modules)[xp-framework/compiler

XP Compiler

2026.0k9](/packages/xp-framework-compiler)

PHPackages © 2026

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