PHPackages                             fas/di - 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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. fas/di

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

fas/di
======

Fast and simple dependency injection

0.4.5(4y ago)02071MITPHPPHP &gt;=7.4.0

Since May 24Pushed 4y ago1 watchersCompare

[ Source](https://github.com/gielfeldt/fas-di)[ Packagist](https://packagist.org/packages/fas/di)[ Docs](https://github.com/gielfeldt/fas-di)[ RSS](/packages/fas-di/feed)WikiDiscussions main Synced 6d ago

READMEChangelogDependencies (9)Versions (22)Used By (1)

[![Build Status](https://github.com/gielfeldt/fas-di/actions/workflows/test.yml/badge.svg)](https://github.com/gielfeldt/fas-di/actions/workflows/test.yml)[![Test Coverage](https://camo.githubusercontent.com/058f869b65340a0bf864ffe6f3e95bafb2417aa65e0df29891ea58bb3635d0eb/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f75726c3d68747470733a2f2f676973742e67697468756275736572636f6e74656e742e636f6d2f6769656c66656c64742f30613163663738646136356439633931643035653661356566316563303830382f7261772f6661732d64695f5f6d61696e2e6a736f6e)](https://camo.githubusercontent.com/058f869b65340a0bf864ffe6f3e95bafb2417aa65e0df29891ea58bb3635d0eb/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f75726c3d68747470733a2f2f676973742e67697468756275736572636f6e74656e742e636f6d2f6769656c66656c64742f30613163663738646136356439633931643035653661356566316563303830382f7261772f6661732d64695f5f6d61696e2e6a736f6e)

[![Latest Stable Version](https://camo.githubusercontent.com/33d0c985cd8673712732f9e7f57bf08fd518d1dd4ce18b9d92f62809f0c0f979/68747470733a2f2f706f7365722e707567782e6f72672f6661732f64692f762f737461626c652e737667)](https://packagist.org/packages/fas/di)[![Latest Unstable Version](https://camo.githubusercontent.com/8c199d1f8a1f8ed0dbf2246a6565fbed8e8124a41dc82f03e6d55a21216819de/68747470733a2f2f706f7365722e707567782e6f72672f6661732f64692f762f756e737461626c652e737667)](https://packagist.org/packages/fas/di#dev-main)[![License](https://camo.githubusercontent.com/a747d79ec448477ecd80db492aa6df10b1e6a0bdb4484b1a214df34dc942af9a/68747470733a2f2f706f7365722e707567782e6f72672f6661732f64692f6c6963656e73652e737667)](https://github.com/gielfeldt/fas-di/blob/main/LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/0a5df62eaae808c57c2b2fee7a373a1e55c5f66249b5e8822387cb7704204781/68747470733a2f2f706f7365722e707567782e6f72672f6661732f64692f646f776e6c6f6164732e737667)](https://camo.githubusercontent.com/0a5df62eaae808c57c2b2fee7a373a1e55c5f66249b5e8822387cb7704204781/68747470733a2f2f706f7365722e707567782e6f72672f6661732f64692f646f776e6c6f6164732e737667)

Installation
============

[](#installation)

```
composer require fas/di
```

Usage
=====

[](#usage)

Creating container
------------------

[](#creating-container)

```
// Brand new container
$container = new Container;

// Load compiled container if present
$container = Container::load('/tmp/container.php');
```

Adding dependencies
-------------------

[](#adding-dependencies)

```
// ->set(entryName, entryName | callback | null)
// (singleton by default)
$container->set(LoggerInterface::class, 'some_container_entry');

// abstract factory
$container->set(LoggerInterface::class, 'some_container_entry')->factory();

// lazy
$container->set(LoggerInterface::class, 'some_container_entry')->lazy(LoggerInterface::class);

// lazy abstract factory
$container->set(LoggerInterface::class, 'some_container_entry')
    ->lazy(LoggerInterface::class)
    ->factory();

// shorthands
$container->singleton(LoggerInterface::class, 'some_container_entry');
$container->factory(LoggerInterface::class, 'some_container_entry');
$container->lazy(LoggerInterface::class, 'some_container_entry');

 // If entry MyLogger::class does not exist in the container,
 // The class MyLogger::class will be instantiated.
$container->singleton(LoggerInterface::class, MyLogger::class);

// Custom factory method
$container->singleton(LoggerInterface::class, function () {
    return new MyLogger;
});

// Any callable will do
$container->singleton(LoggerInterface::class, [MyLoggerFactory::class, 'create']);
```

Abstract factories. Will be resolved on every -&gt;get()

```
// ->factory(entryName, entryName | callback | null)
$container->factory(MyLogger::class); // abstract factory shorthand

$logger1 = $container->get(MyLogger::class); // will create new object
$logger2 = $container->get(MyLogger::class); // will create new object
```

Lazy. Will not be resolved before used. (virtual proxy)

```
// ->lazy(entryName, entryName | callback | null)
$container->lazy(MyLogger::class); // Lazy shorthand

// make anything lazy
$container->set('myentry', [MyFactory::class, 'create'])
    ->lazy(LoggerInterface::class);
```

Mix and match as you please:

```
$container->factory(MyLogger::class)->lazy();
```

equivalent to:

```
$container->lazy(MyLogger::class)->factory();
```

Performance
-----------

[](#performance)

```
// Use cached virtual proxies (lazy), and write cache if missing
$container->enableProxyCache('/tmp/proxies');

// Generate a class containing proper methods for registered entries.
// This can be used afterwards to avoid a lot of reflection when resolving entries.
$container->save('/tmp/container.php');
```

Recipies
========

[](#recipies)

Full automatic cached container and proxies
-------------------------------------------

[](#full-automatic-cached-container-and-proxies)

The easist way to make use of caching.

Be aware, that once a cache has been written, it has to be manually deleted in order to be renewed. This setup is usually not very useful in development.

```
$container = Container::load("/tmp/container.php");
if (!$container) {
    $container = new Container;
    $container->singleton(LoggerInterface::class, MyLogger::class);
    $container->save('/tmp/container.php');
}
$container->enableProxyCache("/tmp/proxies");

// Container ready for use
```

Generate compiled container
---------------------------

[](#generate-compiled-container)

This can be used in a compile script during startup of build phase.

```
$container = new Container;

// ... populate container here

$entries = $container->save("/tmp/container.php");

print "-----------------\nCompiled " . count($entries) . " entries\n-----------------\n";
print implode("\n", $entries) . "\n\n";
```

Generic example
---------------

[](#generic-example)

Generic example using configuration, container and router.

```
