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

ActiveLibrary

danack/injector
===============

Danack/Injector is a recursive auto-wiring dependency injector.

0.9.0(4mo ago)021MITPHPPHP &gt;=8.0CI passing

Since Apr 29Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/Danack/Injector)[ Packagist](https://packagist.org/packages/danack/injector)[ Docs](https://github.com/Danack/Injector)[ RSS](/packages/danack-injector/feed)WikiDiscussions dev Synced 1mo ago

READMEChangelogDependencies (3)Versions (5)Used By (0)

Danack/Injector [![Build Status](https://github.com/danack/injector/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/danack/injector/actions)
================================================================================================================================================================

[](#danackinjector-)

Danack/Injector is a recursive dependency injector. Use it to bootstrap and wire together S.O.L.I.D., object-oriented PHP applications.

Other forks
-----------

[](#other-forks)

The original library, `rdlowrey/auryn` is in low maintenance mode. i.e. new features are very unlikely to be added, and new releases to support new versions of PHP are not guaranteed to be timely. Notes on why some features were not added to Auryn are listed [here](excluded_features.md).

There are similar libraries available at:

- [`martin-hughes/auryn`](https://github.com/martin-hughes/auryn) is a fork from this repo and maintains the current namespace and interfaces. It is unlikely to introduce significant new features, instead focussing on bugfixes and testing.
- [`overclokk/auryn`](https://github.com/overclokk/auryn) is a fork from this repo and maintains the current namespace and interfaces. It has added the ability to lazy instantiate dependencies using [`Ocramius/ProxyManager`](https://github.com/Ocramius/ProxyManager).
- [`amphp/injector`](https://github.com/amphp/injector) is a significant rewrite using a new namespace and slightly different interfaces, requiring you to update your code. It will introduce new features and diverge over time from this repo.

##### How It Works

[](#how-it-works)

Among other things, auryn recursively instantiates class dependencies based on the parameter type-hints specified in class constructor signatures. This requires the use of Reflection. You may have heard that "reflection is slow". Let's clear something up: *anything* can be "slow" if you're doing it wrong. Reflection is an order of magnitude faster than disk access and several orders of magnitude faster than retrieving information (for example) from a remote database. Additionally, each reflection offers the opportunity to cache the results if you're worried about speed. auryn caches any reflections it generates to minimize the potential performance impact.

> auryn **is NOT** a Service Locator. DO NOT turn it into one by passing the injector into your application classes. Service Locator is an anti-pattern; it hides class dependencies, makes code more difficult to maintain and makes a liar of your API! You should *only* use an injector for wiring together the disparate parts of your application during your bootstrap phase.

The Guide
---------

[](#the-guide)

**Basic Usage**

- [Basic Instantiation](#basic-instantiation)
- [Injection Definitions](#injection-definitions)
- [Type-Hint Aliasing](#type-hint-aliasing)
- [Non-Class Parameters](#non-class-parameters)
- [Global Parameter Definitions](#global-parameter-definitions)

**Advanced Usage**

- [Instance Sharing](#instance-sharing)
- [Instantiation Delegates](#instantiation-delegates)
- [Prepares and Setter Injection](#prepares-and-setter-injection)
- [Injecting for Execution](#injecting-for-execution)
- [Dependency Resolution](#dependency-resolution)

**Example Use Cases**

- [Avoiding Evil Singletons](#avoiding-evil-singletons)
- [Application Bootstrapping](#app-bootstrapping)

Requirements and Installation
-----------------------------

[](#requirements-and-installation)

- Danack/Injector requires PHP 7.2 or higher.

#### Installation

[](#installation)

###### Github

[](#github)

You can clone the latest Danack/Injector iteration at anytime from the github repository:

```
$ git clone git://github.com/danacj/injector.git
```

###### Composer

[](#composer)

You may also use composer to include auryn as a dependency in your projects `composer.json`. The relevant package is `rdlowrey/auryn`.

Alternatively require the package using composer cli:

```
composer require danack/injector
```

##### Manual Download

[](#manual-download)

Archived tagged release versions are also available for manual download on the project [tags page](https://github.com/rdlowrey/auryn/tags)

##### Running tests

[](#running-tests)

To allow an appropriate version of PHPUnit to be installed across all of the supported versions of PHP, instead of directly depending on PHPUnit, Auryn instead depends on simple-phpunit.

After doing composer update, you need to tell simple-phpunit to install PHPUnit:

```
vendor/bin/simple-phpunit install

vendor/bin/simple-phpunit --version
```

The tests can then be run with the command:

```
vendor/bin/simple-phpunit
```

Basic Usage
-----------

[](#basic-usage)

To start using the injector, simply create a new instance of the `Auryn\Injector` ("the Injector") class:

```
