承接 pkj/dic 相关项目开发

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

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

pkj/dic

Composer 安装命令:

composer require pkj/dic

包简介

Dependency Injection Container.

README 文档

README

Simple dependency injection container (DIC) or otherwise known as Inversion of Control (IoC). DIC helps you sort out dependencies for objects in a nice manner.

Example

$di = new DependencyInjector();


class World {
    private $str;

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

    public function __toString () {
        return $this->str;
    }
}


$di->service('world', function ($string) {
    return new World($string);
}, ['World']);





$di->service('test', function ($number, $thing) {
    return "Hello $thing, this is a number: $number!";
}, [1337, '@world']);


echo $di->service('test');

Outputs:

Hello World, this is a number: 1337!

Documentation

Services

Use the service method to create new services.

  • Argument 1: The name of the service
  • Argument 2: A callable that should return the value of the service when we want to fetch your service. Can return anything from a primitive value to a complex object.
  • Argument 3: Values to pass into the Argument 2's arguments. Use a string starting with @ to inject a service, otherwise you can inject anything. Note that everything starting with a @ is treated as a service.

Configuration

It's possible to configure the DependencyInjector with the config method. You can also define custom global settings using ->config($key, $value).

Listed configuration below:

DependencyInjector::CONF_STATIC_ANALYSIS

For convenience you can set DependencyInjector::CONF_STATIC_ANALYSIS to true, by doing this you don't need to specify arguments in the third argument using the service method.

Example:

// Enable static anlysis using reflection.
$di->config(DependencyInjector::CONF_STATIC_ANALYSIS, true);

// The following code:


$di->service('test', function ($world) {
    return "Hello $world!";
}, ['@world']);


// Can now become:

$di->service('test', function ($world) {
    return "Hello $world!";
});

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-11-28