mts7/php-dependency-injection 问题修复 & 功能扩展

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

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

mts7/php-dependency-injection

最新稳定版本:1.2.5

Composer 安装命令:

composer require mts7/php-dependency-injection

包简介

Simple dependency injection container for PHP

README 文档

README

PHP Dependency Injection Container

codecov

Installation

composer require mts7/php-dependency-injection

Usage

Set

Getting an instantiated object can only happen after the container knows about the class. One way to tell the container about the definition is to use the set method. set can take an alias or fully-qualified class name as the first parameter and a fully-qualified class name, instantiated object, or null as the second parameter.

Full Example

$container = new Container();

// set an alias with a reference to the class name
$container->set('car', Car::class);
$car = $container->get('car');

// set the name to the class name and provide an instantiated object
$container->set(Car::class, new Car());
$car = $container->get(Car::class);

// provide only the name of the class and get an instantiated object
$container->set(Car::class);
$car = $container->get(Car::class);

Load

To make things easier for projects, passing the abstract and concrete values to Container can happen through the load method. Provide an array indexed by the abstract ID (key, class name, etc.) with a value of a concretion.

Load handles a variety of parameters, allowing for most implementations to work out-of-the-box. When both the key and value are present, the list of definitions available adds valid values and their keys. When the config array has no key, load uses the value to determine the key (which should be the fully-qualified class name). Each value goes through validation to determine if the concretion provided is a valid class or object. Check the tests for examples.

Load is best used when combined with a factory that provides auto-loading of a preconfigured list of abstractions and their concretions.

Full Example

// the factory would create a new Container, then call ->load($config) with the appropriate values
$container = ContainerFactory::create();

$car = $container->get(Car::class);
$car->setName('Taco');
echo $car->getName();

Get

When a class has its own dependencies, all dependencies can exist once they are set in the container. The container uses auto-wiring through Reflection to determine which parameters are classes and then instantiate each one.

Full Example

$container = new Container();
$container->load([Color::class, Car::class]);

// there is no need to pass `Color` to the container since all dependencies load automatically
$car = $container->get(Car::class, ['Alice']);
// getting the color comes from Car's dependency rather than an outside influence
$car->getColor()->setHex('#F0F8FF');
echo $car->getName() . ' is ' . $car->getColor()->getHex();

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固