PHPackages                             ryanl/flexer - 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. ryanl/flexer

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

ryanl/flexer
============

Flexer is a powerful PHP library that offers a versatile implementation of the PSR-11 Container interface. Designed to provide a seamless and standardized way to manage dependencies and services in your PHP applications, Flexer empowers developers with a flexible and intuitive container solution.

v0.0.1(1y ago)013MITPHPPHP ^8.2

Since Jun 26Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ryanlimadotdev/flexer)[ Packagist](https://packagist.org/packages/ryanl/flexer)[ RSS](/packages/ryanl-flexer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

Flexer
======

[](#flexer)

[![Version](https://camo.githubusercontent.com/d966677297b34ac6f35beff609dae565e42033dd29a61bc80eedd1313c97b862/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7279616e6c2f666c65786572)](https://camo.githubusercontent.com/d966677297b34ac6f35beff609dae565e42033dd29a61bc80eedd1313c97b862/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7279616e6c2f666c65786572)[![PHP Version](https://camo.githubusercontent.com/6e01580e49483249a2fdcb851a286434f7d0107da7c935f3dcbbf932ba3cc0f0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7279616e6c2f666c65786572)](https://camo.githubusercontent.com/6e01580e49483249a2fdcb851a286434f7d0107da7c935f3dcbbf932ba3cc0f0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7279616e6c2f666c65786572)[![Total Downloads](https://camo.githubusercontent.com/146a20e8db5bd3b690a981f7136042536d929cc7957e4ac5aa2cc8108a62d7a0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7279616e6c2f666c65786572)](https://camo.githubusercontent.com/146a20e8db5bd3b690a981f7136042536d929cc7957e4ac5aa2cc8108a62d7a0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7279616e6c2f666c65786572)[![License](https://camo.githubusercontent.com/0ea60c21ceaa5cfc0cf3f8acd28622cf500e5b6598f9aa07ab1281d6f1333d05/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7279616e6c2f666c65786572)](https://camo.githubusercontent.com/0ea60c21ceaa5cfc0cf3f8acd28622cf500e5b6598f9aa07ab1281d6f1333d05/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7279616e6c2f666c65786572)

### Description:

[](#description)

A simple, but robust implementation of PSR11, that support multiple types of definitions for your dependencies
--------------------------------------------------------------------------------------------------------------

[](#a-simple-but-robust-implementation-of-psr11-that-support-multiple-types-of-definitions-foryour-dependencies)

Getting Started
---------------

[](#getting-started)

Similar to what is done with PHP-DI

Installation
------------

[](#installation)

Install Flexer with Composer:

```
composer require ryanl/flexer
```

---

Usage
-----

[](#usage)

Instantiate an instance of the container:

```
$container = new \Flexer\Container();
```

With autowiring, Flexer can handle most cases of object instantiation. Here is a list of cases Flexer can cover with *zero configuration*.

### Classes with:

[](#classes-with)

- No constructor
- Empty constructor
- Constructor with optional parameters
- Constructor parameters that are objects of classes fitting the previous cases.

Defining How to Instantiate Complex Objects
-------------------------------------------

[](#defining-how-to-instantiate-complex-objects)

There are two possible syntaxes for adding definitions to Flexer:

### Directly in the constructor:

[](#directly-in-the-constructor)

```
$container = new \Flexer\Container([
   $id => $definition
]);
```

### Using the `add` method:

[](#using-the-add-method)

```
$container = new \Flexer\Container();
$container->add($id, $definition)
```

### Id

[](#id)

The id can be, as defined in PSR-11, the "Identifier of the entry to look for", so it can be an arbitrary identifier. However, the most common practice is to use the fully qualified class name (including its namespace).

```
$container = new \Flexer\Container();
$container->add(\Test\Sample\NormalClass::class, $definition)
```

### Definition

[](#definition)

The definition is the instruction given to the container to instantiate your class. Currently, Flexer accepts the following types of `$definition`:

- Closure

```
$definition = fn() => new \Test\Sample\NormalClass();
$container->add($id, $definition);
```

- Function Name

```
function getNormalClassInstance(): \Test\Sample\NormalClass
{
    return new \Test\Sample\NormalClass();
}
$container->add($id, 'getNormalClassInstance');
```

- Class Closure Object

```
$container->add($id, \Test\Sample\NormalClass::create(...));
```

- Object Instance

```
$container->add($id, new \Test\Sample\NormalClass());
```

> Despite the numerous possibilities for defining your dependencies, we recommend using deferred alternatives, where the dependency is only instantiated when it becomes necessary, avoiding unnecessary memory allocation.

Tip
---

[](#tip)

A more elegant way to manage your definitions is to separate them into a file that provides an array of definitions:

```
# dependencies.php
