承接 minusfour/phpdicontainer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

minusfour/phpdicontainer

最新稳定版本:v1.0.0

Composer 安装命令:

composer require minusfour/phpdicontainer

包简介

Simple DI Container

README 文档

README

Very simple Dependency Injector Container.

Installing

You can install it through composer. Through the CLI:

composer require minusfour/phpdicontainer

Don't forget to add the autoloader to your bootstrap file:

require 'vendor/autoload.php';

How to use

Start by specifying the class namespace:

use MinusFour\DIContainer\Container;
use MinusFour\DIContainer\Injector;

Instantiate the container.

$container = new Container();

Register an injector. Assuming class A depends on class B.

$container->registerInjector(new Injector('A', function($deps){
    return new A($deps['B']);
}, ['B']));

And register the injector dependencies. In this case B.

$container->registerInjector(new  Injector('B', function($deps){
    return new A($deps['B']);
}));

To resolve a class, all you need to do is:

$A = $container->resolve('A');

It will instantiate A and inject its dependencies through their injectors.

Note

The injector constructor takes three parameters. The first parameter must be the name of the class (or dependency), the third parameter is an array containing a list of dependencies (names must match first parameter). The second parameter is an anonymous function that takes one parameter, the dependencies as an array of objects and is responsible for both instantiation and dependency injection.

The injector implementation borrows from pimple examples, mostly the anonymous function idea.

Exceptions note

$container->resolve() is responsible for throwing 2 kind of exceptions: MinusFour\DIContainer\InjectorNotFoundException and MinusFour\DIContainer\DependencyUnmetException. First only occurs if there's no injector for said class, second one is thrown when the container fails to find the injector of the dependency.

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2015-05-17