PHPackages                             timostamm/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. timostamm/injector

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

timostamm/injector
==================

v1.0.0(8y ago)131MITPHPPHP ^7.1.3

Since May 9Pushed 7y ago2 watchersCompare

[ Source](https://github.com/timostamm/injector)[ Packagist](https://packagist.org/packages/timostamm/injector)[ RSS](/packages/timostamm-injector/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

PHP Dependency Injector
-----------------------

[](#php-dependency-injector)

[![Build Status](https://camo.githubusercontent.com/200792c82d4993e44a61c3920b74d39c6b03478a1c187b1695d7bfc189403db0/68747470733a2f2f6170692e7472617669732d63692e6f72672f74696d6f7374616d6d2f696e6a6563746f722e706e67)](https://travis-ci.org/timostamm/injector)

A basic autowiring / autoloading injector with good support for argument overriding.

The injector supports constructor injection and function argument injection:

```
// create an instance
$injector->instantiate(MyClass::class, [
    '$timeout' => 1000
]);

// call a function
$injector->invoke([$object, 'method']);

```

If an argument is type-hinted as a class, the injector automatically creates an instance of this class.

To control how and which values will be injected, you can create aliases, default parameters, singletons and decorators.

##### Alias

[](#alias)

```
// configure injector to provide an instance of MyImplementation
// for every MyInterface
$injector->alias(MyInterface::class, MyImplementation::class);

class Test {
    function __construct(MyInterface $my) {
        print "Test constructed with " . get_class($my);
    }
}

```

##### Singleton

[](#singleton)

```
// configure injector to create only one instance
$injector->singleton(MyImplementation::class)

// prints "bool(true)"
$b = $injector->instantiate(Test::class);
$a = $injector->instantiate(Test::class);
var_dump($a === $b);

```

##### Factories

[](#factories)

```
// configure injector to call the factory function
// to create the object
$injector->factory(Service::class, function(Database $db){
    $total = $db->countEntries();
    return new MyService( $total );
});

$service = $injector->instantiate(Service::class);

```

##### Decorators

[](#decorators)

```
// configure injector to call the decorator function
// after the object is created
$injector->decorate(Service::class, function(Service $service, Logger $logger){
    $service->logger = $logger;
});

$service = $injector->instantiate(Service::class);
$service->logger; // is set

```

##### Parameters

[](#parameters)

```
// set a parameter value by name
$injector->defaults(Service::class, [
    '$timeout' => 1000
]);

// set a value for a type-hinted argument
$injector->defaults(Test::class, [
    MyInterface::class, new MyService()
]);

// set an alias for a type-hinted argument
$injector->defaults(Test::class, [
    MyInterface::class, MyService::class
]);

// set an alias for a type-hinted argument
$injector->defaults(Test::class, [
    MyInterface::class, MyService::class
]);

// provide a type hint for an untyped argument
$injector->defaults(Test::class, [
    'hint $mixed', MyService::class
]);

// set a rest-parameter
$injector->defaults(Service::class, [
    '...$restParam' => [1,2,3]
]):

// set a parameters array
$injector->defaults(Service::class, [1000, "hello"]):

// set a parameter value by index
$injector->defaults(Service::class, [
    '#0' => 1000
]);

```

Parameter configuration do not have to be complete. It is possible to provide or override arguments later:

```
$injector->instantiate(Service::class, [
    '$timeout' => 1000
]);

$injector->invoke([$myService, 'method'], [
    '$foo' => 'bar',
    'hint $other' => Service::class
]);

```

##### Error handling

[](#error-handling)

Parameter configurations, aliases and decorators are strictly validated.

- If you provide undefined parameters, and exception is thrown.
- If your type hint is not assignable to an existing type hint, an exception is thrown.
- If your alias is not assignable, an exception is thrown.
- If you alias or otherwise configure a class that has already been instantiated as a singleton, an exception is thrown.
- If you add an alias that for a class that you have decorated (the decorator would never activate), an exception ist thrown.
- If your factory has a return type, it is checked that the return type is assignable.

##### Argument inspection

[](#argument-inspection)

The InspectableInjectorInterface provides methods to check for missing arguments before invocation:

```
function inspectInvocation(callable $callable): ArgumentInspectionInterface;
function inspectInstantiation(string $className): ArgumentInspectionInterface;

```

This can be used to provide route parameters to a controller, for example.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

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

Total

2

Last Release

2922d ago

Major Versions

v0.1.0 → v1.0.02018-05-09

### Community

Maintainers

![](https://www.gravatar.com/avatar/6364011a12f22cb5046056371bde6170218218c560eddfdc08b4644b7648af78?d=identicon)[timostamm](/maintainers/timostamm)

---

Top Contributors

[![timostamm](https://avatars.githubusercontent.com/u/4289451?v=4)](https://github.com/timostamm "timostamm (23 commits)")

---

Tags

autowiringinjectorphp

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[symfony/dependency-injection

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

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

The Illuminate Contracts package.

704122.9M10.0k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31278.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.9k17](/packages/civicrm-civicrm-core)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)

PHPackages © 2026

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