PHPackages                             yjx/easy-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. yjx/easy-di

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

yjx/easy-di
===========

An easy dependencies inject container, which implements Psr-11.

v1.0.3(7y ago)2261MITPHPPHP ^5.4 || ^7.0

Since Apr 25Pushed 7y ago1 watchersCompare

[ Source](https://github.com/youjiaxing/EasyDI)[ Packagist](https://packagist.org/packages/yjx/easy-di)[ RSS](/packages/yjx-easy-di/feed)WikiDiscussions master Synced 2d ago

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

EasyDI
======

[](#easydi)

EasyDI是一个具有自动依赖注入的小型容器, 遵循PSR-11.

具体详细介绍及讨论请[参见blog](https://blog.csdn.net/love123i/article/details/80088896)

---

容器提供方法:

- `raw(string $id, mixed $value)`
    适用于保存参数, `$value`可以是任何类型, 容器不会对其进行解析.
- `set(string $id, \Closure|array|string $value, array $params=[], bool $shared=false)` 定义服务
- `singleton(string $id, \Closure|array|string $value, array $params=[])` 等同调用`set($id, $value, $params, true)`
- `has(string $id)` 判断容器是否包含$id对应条目
- `get(string $id, array $params = [])`
    从容器中获取$id对应条目, 可选参数$params可优先参与到条目实例化过程中的依赖注入
- `call(callable $function, array $params=[])` 利用容器来调用callable, 由容器自动注入依赖.
- `unset(string $id)` 从容器中移除$id对应条目

安装 Installation
===============

[](#安装-installation)

Install EasyDi with [Composer](http://getcomposer.org/doc/00-intro.html)

`composer require yjx/easy-di`

基础使用 Basic Usage
================

[](#基础使用-basic-usage)

容器的实例化
------

[](#容器的实例化)

```
use EasyDi\Container();

$container = new Container();
```

EasyDI容器管理两种类型的数据: 服务 和 参数(raw)

定义服务和参数
-------

[](#定义服务和参数)

```
use Psr\Container\ContainerInterface;
use EasyDI\Container();

$c = new EasyDI\Container();

// 定义参数
$c->raw('redis.host', "127.0.0.1");

// 使用闭包
$c->set('redis', function(ContainerInterface $c) {
  $redis = new Redis();
  $redis->pconnect($c->get('redis.host'));
  return $redis;
}, [], true);

$reids = $c->get('redis');  // 由于set时第4个参数$shared设置为true, 因此每次获取的都是同一个对象
```

> 闭包中的参数`$c`由于使用了类型指示, 因此EasyDI会自动完成依赖注入(将它自身注入).

自动依赖解决
------

[](#自动依赖解决)

### [示例1](http://php-di.org/doc/getting-started.html)

[](#示例1)

> 直接拿PHP-DI官方演示代码

```
class Mailer
{
    public function mail($recipient, $content)
    {
        // send an email to the recipient
    }
}

class UserManager
{
    private $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function register($email, $password)
    {
        // The user just registered, we create his account
        // ...

        // We send him an email to say hello!
        $this->mailer->mail($email, 'Hello and welcome!');
    }
}

$c = new EasyDI\Container();
$userManager = $c->get('UserManager');

// 等价执行
//$mailer = new Mailer();
//$userManager = new UserManager($mailer);
```

### 示例2

[](#示例2)

```
class ClassA
{
    protected $b;
    protected $say;

    public function __construct(ClassB $b, ClassC $c, $say="hello")
    {
        $this->b = $b;
        $this->say = $say;
    }

    public function saySth()
    {
      return "{$this->say} {$this->b->saySth()}";
    }
}

class ClassB
{
    protected $msg;

    public function __construct($msg)
    {
        $this->msg = $msg;
    }

    public function saySth()
    {
        return $this->msg;
    }
}

class ClassC
{
}

// 容器实例化
$c = new EasyDI\Container();

// 最基础的配置方式, 未用到容器的依赖注入特性
$c->set('basic', function () {
    return new ClassA(new ClassB("easy-di"), new ClassC(), "I like");
});
$basicService = $c->get('basic');
echo $basicService->saySth().PHP_EOL;   // 输出: I like easy-di

// 利用容器自动解决依赖
$c->set(ClassB::class, ClassB::class, ['easy-di']);         // 配置ClassB的标量依赖, params 等同配置 ['msg'=>"easy-di"]
$c->set('advance', ClassA::class, [2=>"I really like"]);    // 配置advance服务, params 等同配置 ['say'=>"I really like"]
$advanceService = $c->get('advance');                       // ClassA实例化所需的第2个参数$c由容器自动生成实例
echo $advanceService->saySth().PHP_EOL; // 输出: I really like easy-di
```

### 示例3 `call()`

[](#示例3-call)

```
```php
class UserManager
{
    private $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function register($email, $password)
    {
        // The user just registered, we create his account
        // ...

        // We send him an email to say hello!
        $this->mailer->mail($email, 'Hello and welcome!');
    }

    public function quickSend(Mailer $mailer, $email, $password)
    {
        $mailer->mail($email, 'Hello and welcome!');
    }
}

function testFunc(UserManager $manager)
{
    return "test";
}

// 实例化容器
$c = new EasyDI\Container();

// 输出: 'test'
echo $c->call('testFunc')."\n";

// 输出: 'test'
echo $c->call(function (UserManager $tmp) {
    return 'test';
});

// 自动实例化UserManager对象	[$className, $methodName]
$c->call([UserManager::class, 'register'], ['password'=>123, 'email'=>'1@1.1']);

// 自动实例化UserManager对象	$methodFullName
$c->call(UserManager::class.'::'.'register', ['password'=>123, 'email'=>'1@1.1']);

// 调用类的静态方法	[$className, $staticMethodName]
$c->call([UserManager::class, 'quickSend'], ['password'=>123, 'email'=>'1@1.1']);

// 使用字符串调用类的静态方法 $staticMethodFullName
$c->call(UserManager::class.'::'.'quickSend', ['password'=>123, 'email'=>'1@1.1']);

// [$obj, $methodName]
$c->call([new UserManager(new Mailer()), 'register'], ['password'=>123, 'email'=>'1@1.1']);

// [$obj, $staticMethodName]
$c->call([new UserManager(new Mailer()), 'quickSend'], ['password'=>123, 'email'=>'1@1.1']);
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 68.8% 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

Every ~36 days

Total

4

Last Release

2830d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d88903546a67008b116b78c326f5ff16ce977b0de9ab21086a8b1b9e6080076c?d=identicon)[youjiaxing](/maintainers/youjiaxing)

---

Top Contributors

[![youjiaxing](https://avatars.githubusercontent.com/u/38421510?v=4)](https://github.com/youjiaxing "youjiaxing (11 commits)")[![love123i](https://avatars.githubusercontent.com/u/18715908?v=4)](https://github.com/love123i "love123i (5 commits)")

---

Tags

containercontainer-interopdependency-injectiondiiocioc-containerpsr-11psr11containercontainer-interopPSR-11dependency-injectiondiiocpsr11

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/yjx-easy-di/health.svg)

```
[![Health](https://phpackages.com/badges/yjx-easy-di/health.svg)](https://phpackages.com/packages/yjx-easy-di)
```

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k48.9M994](/packages/php-di-php-di)[slince/di

A flexible dependency injection container

20260.4k6](/packages/slince-di)

PHPackages © 2026

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