PHPackages                             beige/psr-11 - 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. beige/psr-11

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

beige/psr-11
============

The container implementation of PSR-11.

1.0.0(6y ago)0111MITPHPPHP &gt;=7.1.0

Since Aug 1Pushed 6y ago1 watchersCompare

[ Source](https://github.com/speed-sonic/beige-psr-11)[ Packagist](https://packagist.org/packages/beige/psr-11)[ RSS](/packages/beige-psr-11/feed)WikiDiscussions master Synced 2mo ago

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

Beige PSR-11
============

[](#beige-psr-11)

[![GitHub license](https://camo.githubusercontent.com/d8699554f7186f5447b96dfee3e972a31d2c7143a944fa0ad785407cd64a70a4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f616c69656e776f772f536e6f774c656f706172642e737667)](https://github.com/alienwow/SnowLeopard/blob/master/LICENSE)[![LICENSE](https://camo.githubusercontent.com/be80b8cb211ceb2263744e99fdb161a40124901906fd7c7f47d6361760dd7e8b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d416e74692532303939362d626c75652e737667)](https://github.com/996icu/996.ICU/blob/master/LICENSE)[![Coverage 100%](https://camo.githubusercontent.com/964e6727b820bf4bd13789e84922b6359a9493323aa7d455ec675f1cf403209c/68747470733a2f2f696d672e736869656c64732e696f2f617a7572652d6465766f70732f636f7665726167652f7377656c6c6162792f6f70656e736f757263652f32352e737667)](https://github.com/speed-sonic/beige-route)

一个 PSR-11 标准容器
--------------

[](#一个-psr-11-标准容器)

The container implementation of PSR-11.

简介
--

[](#简介)

Beige PSR-11 是一个轻量级的 PSR-11 标准容器。它的目标是将容器的使用变得更加简单和纯粹，它能够接受任意类型的数据，并将 Definition 分离。

安装
--

[](#安装)

```
composer require beige/psr-11

```

使用
--

[](#使用)

引入并创建容器的实例：

```
use Beige\Psr11\Container;

$container = new Container();
```

### 读写操作

[](#读写操作)

可以在容器初始化的时候向容器写入数据，数据项的索引必须是字符串类型：

```
$container = new Container([
    'foo' => 'bar'
]);
```

`Beige\Psr11\Container` 实现了 `ArrayAccess` 接口，支持以数组的形式操作数据：

```
$container['foo'] = 'bar';
isset($container['foo']);  // true
unset($container['foo']);
```

如果根据索引无法找到数据，Container 会抛出一个 `Beige\Psr11\Exception\NotFoundException` 异常

### 定义 (Definition)

[](#定义-definition)

“定义”是一个可调用对象，它定义了生成一个数据的具体过程，并返回数据本身。容器可以利用“定义”来生成最终的数据。这里，我们使用 `Beige\Psr11\DefinitionCollection` 保存一个定义的集合，传入 container 中：

```
$definitionCollection = new DefinitionCollection([
    'foo' => function($container) {
        return 'bar';
    }
]);

$container = new Container([], $definitionCollection);
$container['foo'];  // bar
```

`Beige\Psr11\Container` 构造函数的第二个参数可选，接受一个 `Beige\Psr11\Interfaces\DefinitionCollectionInterface` 接口的实例，当 Container 找不到某个索引的数据时，会查询 DefinitionCollection 中 对这个索引的“定义”，如果存在定义，容器就会利用定义生成具体数据并保存在容器中。Container 将延用 Definition 的索引。

`Beige\Psr11\DefinitionCollection` 与容器对象一样支持数组形式的操作：

```
use Beige\Psr11\Container;
...

$definitionCollection['foo'] = function(Container $c) {
    return 'bar';
};
```

在默认情况下，Container 会把自身传给 Definition 的第一个参数，如上例所示，这样，你就可以在 Definition 内部获取容器中的其他数据，帮助当前的 Definition 生成数据。当然，你也可以定义一个“非常规”的 Definition，并用 `Beige\Psr11\Container::make` 方法生成数据：

```
$definitionCollection['foo'] = function($num1, $num2) {
    return $num1 + $num2;
};

$container = new Container([], $definitionCollection);
$container->make('foo', [1, 2]);  // 3
```

`Beige\Psr11\Container::make` 方法的第一个参数是 Definition 的索引 *（注意：不是容器数据的索引）*；第二个参数是一个数组，是传给 Definition 的参数列表。 用 make 方法生成的数据会直接返回，且不会保存在容器中，也就是说，每调用一次 make 方法，都会重新生成一次数据！

使用 Definition 可以在需要时才生成数据，省却了不必要的消耗。推荐使用者将初始化消耗较大的数据转化为 Definition.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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

2474d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9f4cfe2ff9c9a5fe469dd2701b7c22132e5b5a9e053a32c54d7ae90cfe565ef3?d=identicon)[Alex Layton](/maintainers/Alex%20Layton)

---

Top Contributors

[![alex-1900](https://avatars.githubusercontent.com/u/49949411?v=4)](https://github.com/alex-1900 "alex-1900 (5 commits)")

---

Tags

containerPSR-11psr11

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/beige-psr-11/health.svg)

```
[![Health](https://phpackages.com/badges/beige-psr-11/health.svg)](https://phpackages.com/packages/beige-psr-11)
```

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k48.9M991](/packages/php-di-php-di)[phpwatch/simple-container

A fast and minimal PSR-11 compatible Dependency Injection Container with array-syntax and without auto-wiring

1810.1k2](/packages/phpwatch-simple-container)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
