kanian/containerx 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

kanian/containerx

Composer 安装命令:

composer require kanian/containerx

包简介

A simple PHP DI container

README 文档

README

ContainerX is a little PHP dependency injection container.

Installation

$ composer require kanian/containerx

Usage

Let:

The Car class be:

class Car {
	protected $driver;
    public function __construct(Driver $driver)
    {
    	$this -> driver = $driver;
    }
    \\\ ... more car code
}

And the HumanDriver class be:

class HumanDriver implements Driver {
  public function drive()
  {
  	\\\ ... some driving code
  }
}

We can use:

Container functionalities as object methods

In order to access the functionalities of the container as object methods:

use Kanian\ContainerX\Container;

$container = new Container();
$container->set('chauffeur', function($c){ return new HumanDriver;});
$container->set('limo', function($c){ return new Car($c->get('chauffeur'));});

$limo = $container->get('limo');

We have used anonymous functions has factories. Moreover, We could simply register the dependencies we need and let the container instantiate them:

use Kanian\ContainerX\Container;
$container = new Container();
$container->set('chauffeur',HumanDriver::class);
$container->set('limo',Car::class);
$limo = $container->get('limo');

The container will know how to construct a Car instance for us.

Alternatively, we can use:

Container functionalities through the ArrayAccess Interface

For example, we can achieve factory based registration by using the Kanian\Container\ContainerX class, which implements the ArrayAccess interface.

use Kanian\ContainerX\ContainerX;

$container = new ContainerX();
$container['chauffeur'] =  HumanDriver::class;
$container['limo'] = Car::class;

$limo = $container['limo'];

Accessing a dependency as a Singleton

In order to ensure that there is receive only one copy of a dependency in the system at a time, you will use the

singletonize

method. like this:

$container->singletonize('limo', Car::class);
$limo = $container['limo'];

Now you will always get the same instance of Car, but with different instances of HumanDriver..

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-02-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固