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

ActiveLibrary[Framework](/categories/framework)

szepeviktor/injector-php-7
==========================

A dependency injector for bootstrapping object-oriented PHP applications.

v1.5.1(5y ago)5111MITPHPPHP ^7.4

Since Mar 6Pushed 5y ago1 watchersCompare

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

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

injector
========

[](#injector)

A recursive dependency injector to bootstrap and wire together S.O.L.I.D., object-oriented PHP applications.

##### How It Works

[](#how-it-works)

Among other things, the injector 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. The injector caches any reflections it generates to minimize the potential performance impact.

> The injector **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)

- Requires PHP 8.0 or higher.

#### Installation

[](#installation)

###### Composer

[](#composer)

You may also use composer to include the project as a dependency in your projects `composer.json`. The relevant package is `amphp/injector`.

Alternatively require the package using composer cli:

```
composer require amphp/injector
```

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

[](#basic-usage)

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

```
