PHPackages                             dustinfog/canoe-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. [Framework](/categories/framework)
4. /
5. dustinfog/canoe-di

ActiveLibrary[Framework](/categories/framework)

dustinfog/canoe-di
==================

A Simple Ioc Framework

0.0.2(9y ago)6222proprietaryCPHP &gt;=5.5.25

Since Sep 10Pushed 9y ago1 watchersCompare

[ Source](https://github.com/dustinfog/canoe-di)[ Packagist](https://packagist.org/packages/dustinfog/canoe-di)[ Docs](https://github.com/dustinfog/canoe-di)[ RSS](/packages/dustinfog-canoe-di/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

\#CanoeDI 一个非常简单且实用的IoC框架,相对于其他的Ioc框架有如下特点:

1. 高效: 框架使用了非常实用且高效的算法，使得框架本身对应用的影响微乎其微，且框架提供了C扩展，最大限度的将性能提升到最高。
2. 配置简单: 大多数情况下几乎不需要额外的配置
3. 自动装配: 基于PHPDocument的property属性来自动装配
4. 懒加载: 所有被注入的变量的实例都为即用即取, 不会产生内存垃圾
5. IDE友好: 因为利用的是PHP的标准规范, 兼容大部分IDE

\##安装

编译安装，可以得到最大的效率：

```
$ git clone https://github.com/dustinfog/canoe-di.git
$ cd canoe-di/ext
$ phpize
$ ./configure
$ make
$ sudo make install
```

而后编辑php.ini

```
[canoe-di]
extension=canoe_di.so
```

composer安装 (生产环境如果已经编译安装了扩展，此步骤可省略。在开发环境，PHP源码可以让IDE提供代码完成提示，所以仍然推荐执行这一步)：

```
composer require dustinfog/canoe-di
```

\##使用 ###获取实例

```
class ClassA
{
}
//DI容器在处理类型时,会在第一次遇到的时候实例化,并且在以后使用中以单例的方式使用。
$a = \Canoe\DI\Context::get(ClassA::class);
```

\###基于标注的装配

```
class ClassC
{
}

use \Canoe\DI\DITrait;
use \Canoe\DI\Context;
/**
 * @property ClassC $c
 */
class ClassA
{
    //需要引入一个trait,用以处理$c的获取
    use DITrait;

    public function test() {
        //这里可以直接使用
        print_r($this->c);
    }
}

$a = Context::get(ClassA::class);
$a->test(); //试一下会发生什么
```

\###@uses标注： uses可以指定属性使用的类或者容器里的实例

```
interface InterfaceC {
    public function sayHello();
}

class ClassWorld implements InterfaceC
{
    public function sayHello() {
    	echo "hello, world!\n";
    }
}

class ClassC implements InterfaceC
{
	private $name;

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

    public function sayHello() {
    	echo "hello, $name!\n";
    }
}

use \Canoe\DI\DITrait;
use \Canoe\DI\Context;

/**
 * @property InterfaceC $c1 {@uses ClassWorld} //使用类名
 * @property InterfaceC $c2 {@uses c2} //使用容器内的ID
 */
class ClassA
{
    //需要引入一个trait,用以处理$c的获取
    use DITrait;

    public function test() {
        print_r($this->c1);
        print_r($this->c2);
    }
}

Context::set("c2", new ClassC("Bob"));
// 更好的选择：Context::registerDefinition("c2", function(){new ClassC("Bob")})

$a = Context::get(ClassA::class);
$a->test(); //试一下会发生什么
```

### Singleton

[](#singleton)

有时候，我们需要在一个非DI环境里有限的使用DI，这时候每个系统与DI容器的先借点都在调用Context::get()显得很丑陋，框架里提供了一个更加亲民的调用方式：

```
use \Canoe\DI\SingletonTrait;

class ClassA
{
    use SingletonTrait;
}

$a = ClassA::getInstance();
// 与Context::get(ClassA::class)等价，但隐藏了Context调用。

$a = ClassA::getInstance("a1");
// 与Context::get("a1")等价，但做了进一步的类型检查，即a1取到的实例与ClassA必须有"is a"的关系。
```

\###预先定义 上面的例子都是在运行时来实现自动装配的,但在某些时候可能需要手动预先创建一些定 义,以备后续使,框架提供了简单的支持.

```
//注册类
Canoe\DI\Context::registerDefinition('a', ClassA::class);
//注册回调
Canoe\DI\Context::registerDefinition(
	'b',
	function() {
   		return new ClassB();
	}
);
//注册实例
Canoe\DI\Context::set('c', new ClassC());
```

\###配置 大多数时候,预先定义都是写在配置文件里,可以用下列的方法加载配置:

```
\Canoe\DI\Context::loadConfig(
[
    'definitions' => [ //这里是定义
        ClassB::class,
    ],
    'beans' => [ //这里可以预定义一些实际的值
        'uid' => 5,
    ],
]);
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity49

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

Every ~0 days

Total

2

Last Release

3531d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/03f573e7f8dfc01663333b246cbeb0b8eb8902958c3d709f1a408c7266ee74ae?d=identicon)[dustinfog](/maintainers/dustinfog)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dustinfog-canoe-di/health.svg)

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

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

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