PHPackages                             marcosh/fundic - 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. marcosh/fundic

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

marcosh/fundic
==============

0.1.0(8y ago)15PHPPHP ^7.1

Since Aug 30Pushed 8y ago1 watchersCompare

[ Source](https://github.com/marcosh/fundic)[ Packagist](https://packagist.org/packages/marcosh/fundic)[ RSS](/packages/marcosh-fundic/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

Fundic
======

[](#fundic)

[![Latest Stable Version](https://camo.githubusercontent.com/e782ab27b023ccea7e5f361ebf2c696fd1dc2385dd48879ee5bf91895793dc67/68747470733a2f2f706f7365722e707567782e6f72672f6d6172636f73682f66756e6469632f76657273696f6e)](https://packagist.org/packages/marcosh/fundic)[![Build Status](https://camo.githubusercontent.com/1dd858f1a2e569ae3f52c5dc09e192b2f69172dc05906cfae2f732501a821a3c/68747470733a2f2f7472617669732d63692e6f72672f6d6172636f73682f66756e6469632e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/marcosh/fundic)[![Code Climate](https://camo.githubusercontent.com/e205eb8da9a07e91b7ec9486a66b927389cacd82e1d25d11f8dc1260e8047cbb/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6d6172636f73682f66756e6469632f6261646765732f6770612e737667)](https://codeclimate.com/github/marcosh/fundic)[![Coverage Status](https://camo.githubusercontent.com/5005a472ce4912a7c5a1cb0303a58e0a03776ced5e2b762ab0dc1b99419d9bf8/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d6172636f73682f66756e6469632f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/marcosh/fundic?branch=master)[![Codacy Badge](https://camo.githubusercontent.com/73159b3034ae434941d3f476617d3af5661c2f2a440a32373715fd8a6c2db6d6/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3830636664383633646264373434653561663532346339336634373936376134)](https://www.codacy.com/app/marcosh/fundic)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/abf6bfc0bf062729dc3069304221bd97780189750a4aa2af2b91ce7e348a10a3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6172636f73682f66756e6469632f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/marcosh/fundic/?branch=master)

A purely functional Dependency Injection Container for PHP

Install
-------

[](#install)

Add `fundic` as a dependency to your project using [Composer](https://getcomposer.org) running

```
composer require marcosh/fundic
```

Tests
-----

[](#tests)

Run the tests using

```
php vendor\bin\phpunit
```

Theory
------

[](#theory)

In its essence a dependency injection container is just a component which is able, from a key, to retrieve a corresponding working object.

A common approach to do this is to configure how the corresponding object should be contructed or even relay on autowiring based on class naming.

Another approach si to see a dependency injection container as a map that associates to a key a factory which builds the object identified by the key, possibly using recursively the container itself.

`fundic` takes this idea to its core and, in fact, if you look at the essence, it is just a map that associates keys to factories of the form

```
interface ValueFactory
{
    /**
     * @return mixed
     */
    public function __invoke(ContainerInterface $container, string $name);
}
```

Basic usage
-----------

[](#basic-usage)

### Instantiate

[](#instantiate)

Actually `fundic` gives you two containers:

- `Psr11Container` which implements the specifications of [PSR-11](https://github.com/container-interop/fig-standards/blob/master/proposed/container.md);
- `TypedContainer`, a more type safe version

You can create a new instance of them just by calling their static method `create`:

```
$container = TypedContainer::create();
```

This will create an empty instance of the container that you can fill as you like.

### Configure

[](#configure)

An empty container is not really useful. You could add new entries to the container using

```
$container = $container->add($key, $factory);
```

where `$key` is a string and `$factory` is an instance of `ValueFactory`.

Be aware that `$container` is immutable and that `add` returns a new instance. Therefore it is really important that you remember to assign its result to a variable.

### Retrieve

[](#retrieve)

Both `Psr11Container` and `TypedContainer` implement [`Psr\Container\ContainerInterface`](https://github.com/php-fig/container/blob/master/src/ContainerInterface.php)(even if `TypedContainer` is just respecting the signature of the methods and not conforming to the annotations), therefore you can query them using the `set` and `has` methods as follows

```
// create a new empty container
$container = Psr11Container::create();

// instructs the container on how to build the object
// associated with the provided key
$container = $container->add('foo', $factory);

$container->has('foo'); // returns true

$object = $container->get('foo'); // retrieves the object associated to the key
```

Container return values and exceptions
--------------------------------------

[](#container-return-values-and-exceptions)

`Psr11Container` works as a standard container and conforms completely to the specifications of [PSR-11](https://github.com/container-interop/fig-standards/blob/master/proposed/container.md). Hence, the result of any call to the `get` method is the expected object.

`Psr11Container::get` also throws exceptions if a key is not found or if there is an error while building the return value.

On the other hand, `TypedContainer` alone does not totally conform to the specifications of [PSR-11](https://github.com/container-interop/fig-standards/blob/master/proposed/container.md), specifically in the return values of `get` and the handling of the exceptions.

In order to make the code purely functional and to avoid unwanted side effects, the result of `get` is not directly the desired object, but a `Result` data structure which could have the following values:

- `Just`, which is a wrapper around the desired value that can be retrieved using `Just::get`;
- `NotFound`, which represents the fact that such an entry is not present in the container;
- `Exception`, which represents the fact that something wrong happened while invoking the factory;

These above are just values and you could do whatever you want with them (immediately react to them, pass them around, etc ...)

Factories
---------

[](#factories)

Some factories are provided to ease the creation of `Fundic\Factory\ValueFactory` instances.

It goes without saying that you could provide your own ad-hoc implementations of `Fundic\Factory\ValueFactory`.

### ConstantFactory

[](#constantfactory)

If you need to store in the container a constant value, may it be a native data type, an array or an object, you could use the `Fundic\Factory\ConstantFactory` as follows:

```
$value = // your constant that needs to be stored in the container

$container->add('foo', new ConstantFactory($value));

$container->get('foo');
```

The `ConstanctFactory` class wraps the value in a `Fundic\Factory\ValueFactory` which always returns the provided value.

### ClassNameFactory

[](#classnamefactory)

If you need to retrieve from the container an object with no (or only optional) dependencies, you could use a `Fundic\Factory\ClassNameFactory`, passing to it just the class name, as follows:

```
class Foo { ... } // no non optional dependencies in the constructor

$container->add(Foo::class, new ClassNameFactory(Foo::class));

$container->get(Foo::class);
```

The `ClassNameFactory` just calls `new` on the provided class name and returns a new instance of the class.

### Callable factory

[](#callable-factory)

The most generic `Fundic\Factory\ValueFactory` implementation that we provide is `Fundic\Factory\CallableFactory`, which just wraps any callable with the same signature of `ValueFactory` (i.e. it needs to have as input parameters a `Psr\Container\ContainerInterface` and a `string` which is the class name). For example:

```
$callable = function (ContainerInterface $container, string $name) { ... };

$container->add(Foo::class, new CallableFactory($callable));

$container->get(Foo::class);
```

Factory decorators
------------------

[](#factory-decorators)

Sometimes you want to modify how a specific key is built and retrieved from the container without touching the provided factory.

An easy mechanism to allow this possibility is to use the decorator pattern. This means that we wrap our factory with another factory which receives the first factory as a constructor argument. In functional terms, suppose we have a factory `f` for a specific `foo` key (i.e. `f : (ContainerInterface, string) -> foo`); what we do is passing the whole `f` to `g` where `g(f) : (ContainerInterface, string) -> foo`.

This allows us to modify the result of the inner factory before returning it, or even avoiding to call the inner factory and return a newly built value.

You could provide your own factory decorators to create complex workflows for object creations. Decorators are highly composable, so you could use several of them to build a single object.

Some decorators of common use are provided by the library.

### Memoize

[](#memoize)

If you want to retrieve the same instance of an object every time you ask a particular key to the container, you need to store the result obtained the first time somewhere and the return that instead of creating a new instance every time.

This is exactly what the `Memoize` decorator does. The first time it calls the inner factory to build the object, and then always returns that particular instance.

```
class Foo { ... }

$container->add(Foo::class, new Memoize(new ClassNameFactory(Foo::class)));

$container->get(Foo::class); // a new instance of Foo is built and returned
$container->get(Foo::class); // the same instance of Foo is returned
```

### Proxy

[](#proxy)

If the building process of a object is particularly heavy, you could desire to postpone it until the very last moment when you are sure you need an instance of that particular object.

To do this you could proxy your object and initially return a wrapper that will build the actual object only once a method is called on it.

You could do this using the `Proxy` decorator, as follows:

```
class Foo { ... } // class which is heavy to build

$container->add(Foo::class, new Proxy(new ClassNameFactory(Foo::class)));

$foo = $container->get(Foo::class); // returns a proxy

$foo->bar(); // here we really instantiates Foo and call the bar method on it
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

3180d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2643972?v=4)[Marco Perone](/maintainers/marcosh)[@marcosh](https://github.com/marcosh)

---

Tags

container-interopdependency-injectionfunctionalfunctional-programmingphpphp-library

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/marcosh-fundic/health.svg)

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

###  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.

704122.9M10.1k](/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.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)
