phutureproof/guardian

依赖注入器

3.0.0 2016-04-14 22:18 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:04:04 UTC


README

构建状态

主分支

Build Status

开发分支

Build Status

Code Climate

Code Climate

基本用法

安装

通过composer

composer require phutureproof/guardian

或者将以下内容添加到你的composer.json文件中:

"require": {
  "phutureproof/guardian": "~3"
}

或者手动抓取src文件夹,并将文件放置到你希望的位置。

使用方法

创建依赖项并注册函数以返回容器中对象的实例

Guardian::register('dependency.name', function()
{
    return new Dependency();
});

获取依赖项的实例

$instance = Guardian::make('dependency.name');

注册单例

Guardian::register('singleton.dependency.name', function () {
    static $instance;
    if (is_null($instance)) {
        $instance = new Dependency();
    }
    return $instance;
});