PHPackages                             fowlerwill/attribute-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. fowlerwill/attribute-injection

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

fowlerwill/attribute-injection
==============================

PHP Attribute based dependency injection

10PHP

Since Oct 12Pushed 3y ago2 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

PHP Attribute-based Injection.
==============================

[](#php-attribute-based-injection)

Sounds exciting, doesn't it?! Well this is aimed to be a simple container that can *help* you prevent dependency-hell.

Super-quick Getting Started.
----------------------------

[](#super-quick-getting-started)

```
// Map interfaces to concrete classes.
$dependencyMap = [
    MyDependencyInterface::class => MyDependencyPort::class
];

// Pass them to the container.
$container = new Container($dependencyMap);

// Annotate your classes with #[Injected] on properties or constructor params.
class Main
{
    #[Injected]
    protected MyDependencyInterface $dependency;

    protected MyOtherDependencyInterface $otherDependency;

    public function __construct(MyOtherDependencyInterface $otherDependency)
    {
        $this->otherDependency = $otherDependency;
    }
}

$main = $container->make(Main::class);

// Now the dependency will be fulfilled, and you can call methods that rely
// on them.
$main->whateverMethod();
```

Getting started.
----------------

[](#getting-started)

Say you've got a dependency that you'd like to bring in from some third party, what you really should do, is write an interface for the methods you need, and a port that interacts with the dependency. This way the third party dependency is isolated to a single port, and doesn't pollute it's way throughout your code.

The way that this project is meant to be used, is first to require it in your project:

```
$ composer require fowlerwill/attribute-injection
```

Then, you can instantiate the Container, and provide it with your interface map that maps the dependencies to ports.

```
// Somewhere like index.php or during bootstrap of your application.
$dependencyMap = [
    MyDependencyInterface::class => MyDependencyPort::class
];

$container = new Container($dependencyMap);
```

Where `MyDependencyInterface.php` might look like this:

```
interface MyDependencyInterface
{
    public function party(): string;
}
```

and `MyDependencyPort.php` might looks like this:

```
use FowlerWill\AttributeInjection\Injected;
use Thirdparty\Dependency\SomeDependency;

class MyDependencyPort implements MyDependencyInterface
{

    #[Injected]
    protected SomeDependency $dependency;

    public function party(): string
    {
        return "party, " . $this->dependency->partyHard();
    }
}
```

Notice that we've used the PHP Attribute `#[Injected]` there to bring the dependency along for the ride, when we tie it all together with our class that uses the interface...

```
use FowlerWill\AttributeInjection\Injected;

class Main
{

    #[Injected]
    protected MyDependencyInterface $dependency;

    public function itsTimeToParty(): string
    {
        return "I believe it's time to ".$this->dependency->party();
    }
}
```

and back in our application, we instantiate the class via the container, and can use the dependencies that have been provided to the properites in the class.

```
$main = $container->make(Main::class);

echo $main->itsTimeToParty();
```

Contributing.
-------------

[](#contributing)

First, I'm honoured that you'd want to contribute to the project, so thank you for even considering. There are a lot of ideas that I have for the project, but am super open to PR's at this stage to help grow into use cases that you need.

### Getting Started.

[](#getting-started-1)

```
$ composer install
```

### Testing.

[](#testing)

To run the tests locally:

```
$ composer run test
```

To run the tests on docker, with coverage report:

```
$ composer run test-docker
```

###  Health Score

14

—

LowBetter than 1% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity25

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/2776516a486594653e4ac2542aa8c712710e757520a127520c34655b6c9ca8fa?d=identicon)[wfowler](/maintainers/wfowler)

---

Top Contributors

[![fowlerwill](https://avatars.githubusercontent.com/u/4501119?v=4)](https://github.com/fowlerwill "fowlerwill (6 commits)")

---

Tags

attributedependency-injectionphp

### Embed Badge

![Health badge](/badges/fowlerwill-attribute-injection/health.svg)

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

###  Alternatives

[shockwavemk/magento2-module-cron-schedule

Show advanced information for scheduled crons in Magento2 Backend

3533.9k](/packages/shockwavemk-magento2-module-cron-schedule)[sylvainjule/code-editor

Code editor field for Kirby 3, 4 and 5

377.9k](/packages/sylvainjule-code-editor)[runelaenen/sw6-redirects

Redirect plugin for Shopware 6

2511.4k](/packages/runelaenen-sw6-redirects)[iserter/laravel-claude-agents

A collection of Claude AI agents and skills specifically designed for Laravel development

412.3k](/packages/iserter-laravel-claude-agents)

PHPackages © 2026

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