yoshi2889/container

此包已被弃用,不再维护。未建议替代包。

存储对象实例的简单PSR-11容器实现

v0.1.1 2017-07-19 13:20 UTC

README

Build Status Scrutinizer Code Quality Scrutinizer Code Coverage Latest Stable Version Latest Unstable Version Total Downloads

存储对象实例的简单PSR-11容器实现。

安装

您可以通过 composer 安装此类

composer require yoshi2889/container

使用方法

首先,实例化一个新的 ComponentContainer。这是将保存您放入其中的组件引用的对象。

对于可能添加到 ComponentContainer 的任何类,它必须实现 ComponentInterface 接口。可选地,可以使用 ComponentTrait 特性来提供一个现成的实现 ComponentInterface 的方法。

将实例添加到容器后,可以通过在容器上调用 get 方法并提供完整的类名来检索它,或者,更方便地,通过组件上的静态 fromContainer 方法。

使用示例

<?php

class ExampleClass implements \Yoshi2889\Container\ComponentInterface
{
	use \Yoshi2889\Container\ComponentTrait;
	
	public function test()
	{
		echo 'Hello world!';
	}
}

$componentContainer = new \Yoshi2889\Container\ComponentContainer();
$exampleClassInstance = new ExampleClass();

$componentContainer->add($exampleClassInstance);

// echoes 'Hello world!'
ExampleClass::fromContainer($componentContainer)->test();

许可

此代码在 MIT 许可下发布。请参阅 LICENSE 文件以阅读许可。