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

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

zalas/injector
==============

Injects services from a PSR-11 service container into objects

v2.7.0(8mo ago)5411.6k—8.9%32MITPHPPHP ^8.1CI passing

Since Mar 2Pushed 8mo ago2 watchersCompare

[ Source](https://github.com/jakzal/injector)[ Packagist](https://packagist.org/packages/zalas/injector)[ GitHub Sponsors](https://github.com/jakzal)[ RSS](/packages/zalas-injector/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (21)Used By (2)

Injector
========

[](#injector)

[![Build](https://github.com/jakzal/injector/actions/workflows/build.yml/badge.svg)](https://github.com/jakzal/injector/actions/workflows/build.yml)

Injects services from a [PSR-11 dependency injection container](https://github.com/php-fig/container) into objects. Service information is read from class properties annotated with `@inject`, but extension points are provided to read them from any source.

Example of a class that default implementation of injector can work with:

```
class Foo
{
    /**
     * @inject
     */
    private Service1 $service1;

    /**
     * @inject foo.service2
     */
    private Service2 $service2;
}
```

Why?
----

[](#why)

The library is useful in situations when we have no control over how objects are instantiated, so we can't use proper dependency injection. One use case is feeding services from an application container to integration test cases. Test cases are instantiated by a test framework, so it's not possible to provide additional dependencies during construction time. However, since test frameworks usually give ways to hook into the initialization process, it's still possible to provide additional dependencies before test cases are called.

PHPUnit integration is actually implemented in [`zalas/phpunit-injector`](https://packagist.org/packages/zalas/phpunit-injector) (github repository: ).

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

[](#installation)

```
composer require zalas/injector
```

Usage
-----

[](#usage)

Properties should be annotated with `@inject` in order for them to be recognised by the default injector implementation:

```
class Foo
{
    /**
     * @inject
     */
    private Service1 $service1;

    /**
     * @inject foo.service2
     */
    private Service2 $service2;

    public function hasService1(): bool
    {
        return $this->service1 instanceof Service1;
    }

    public function hasService2(): bool
    {
        return $this->service2 instanceof Service2;
    }
}

class Service1
{
}

class Service2
{
}
```

The injector can be used to feed services into properties of an already instantiated object:

```
use Zalas\Injector\Service\Injector;
use Zalas\Injector\Factory\DefaultContainerFactory;
use Zalas\Injector\Factory\DefaultExtractorFactory;

$foo = new Foo();
$container = /* create / fetch your container */;
$injector = new Injector(new DefaultContainerFactory($container), new DefaultExtractorFactory());
$injector->inject($foo);

var_dump($foo->hasService1());
// bool(true)
var_dump($foo->hasService2());
// bool(true)
```

Extension points
----------------

[](#extension-points)

`Zalas\Injector\Service\Injector` injects services to objects. Services are provided by a PSR-11 container (`Psr\Container\ContainerInterface`). Details of services to inject are read with an extractor (`Zalas\Injector\Service\Extractor`).

Both collaborators are accessed via their factories (`Zalas\Injector\Service\ContainerFactory`and `Zalas\Injector\Service\ExtractorFactory`).

The injector provides two extension points:

- container factory - allows to change the way container is created
- extractor &amp; extractor factory - allow to provide a custom way of extracting definitions of services to inject

### Container factory

[](#container-factory)

The `Zalas\Injector\Factory\DefaultContainerFactory` is a default factory implementation that always returns an instance of container created externally.

The `Zalas\Injector\Service\ContainerFactory` interface needs to be implemented to provide customised way of creating the service container to be used with injector.

### Extractor &amp; extractor factory

[](#extractor--extractor-factory)

The default implementation of extractor (`Zalas\Injector\Reflection\ReflectionExtractor`) leverages PHP's reflection for annotation parsing. It uses the `@inject` annotation to read service information. The annotation accepts service id as an optional value. Otherwise the type is used. `Zalas\Injector\Factory\DefaultExtractorFactory` creates this default implementation of extractor.

Implement the `Zalas\Injector\Service\Extractor` interface to support your own way of extracting definitions of services to inject. A factory that implements the `Zalas\Injector\Service\ExtractorFactory` will also need to be created to instantiate the custom extractor.

Contributing
------------

[](#contributing)

Please read the [Contributing guide](CONTRIBUTING.md) to learn about contributing to this project. Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance58

Moderate activity, may be stable

Popularity42

Moderate usage in the ecosystem

Community19

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 90.4% 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 ~144 days

Recently: every ~245 days

Total

20

Last Release

266d ago

Major Versions

v1.3.1 → v2.0.02020-02-12

PHP version history (10 changes)v1.0.0PHP ^7.1

v1.1.0PHP ^7.2

v1.2.1PHP ^7.2,&lt;8.0

v2.0.0PHP ^7.4,&lt;8.0

v2.1.0PHP ^7.4 || ~8.0.0

v2.2.0PHP ~8.0.0 || ~8.1.0

v2.4.0PHP ~8.0.0 || ~8.1.0 || ~8.2.0

v2.5.0PHP ~8.1.0 || ~8.2.0

v2.6.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0

v2.7.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/012270929478a287f88353135a39d275db09c1856d344d0245e04df709879262?d=identicon)[jakzal](/maintainers/jakzal)

---

Top Contributors

[![jakzal](https://avatars.githubusercontent.com/u/190447?v=4)](https://github.com/jakzal "jakzal (113 commits)")[![gquemener](https://avatars.githubusercontent.com/u/668604?v=4)](https://github.com/gquemener "gquemener (8 commits)")[![OndraM](https://avatars.githubusercontent.com/u/793041?v=4)](https://github.com/OndraM "OndraM (2 commits)")[![gjuric](https://avatars.githubusercontent.com/u/223015?v=4)](https://github.com/gjuric "gjuric (1 commits)")[![grossmannmartin](https://avatars.githubusercontent.com/u/1177414?v=4)](https://github.com/grossmannmartin "grossmannmartin (1 commits)")

---

Tags

annotationsinject-servicesinjectorphppsr-11

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/zalas-injector/health.svg)

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

###  Alternatives

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k431.1M7.5k](/packages/symfony-dependency-injection)[illuminate/contracts

The Illuminate Contracts package.

705122.9M10.1k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31178.1M2.0k](/packages/illuminate-container)[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)

PHPackages © 2026

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