minphp/container
最新稳定版本:2.1.0
Composer 安装命令:
composer require minphp/container
包简介
A Dependency Injection Container based on Pimple
README 文档
README
A standards compliant Pimple-based container.
Installation
Install via composer:
composer require minphp/container:~2.0
Basic Usage
<?php use Minphp\Container\Container; $container = new Container(); // Add/Update $container->set('queue', function($c) { return new \SplQueue(); }); // Verify $queue_exists = $container->has('queue'); // Fetch $queue = $container->get('queue'); // Remove $container->remove('queue');
You can also use the container as an array, as per Pimple. Though, using the ContainerInterface defined methods is preferable as it eases switching to a different container if you ever need to.
<?php use Minphp\Container\Container; $container = new Container(); $container['queue'] = function($c) { return new \SplQueue(); }; $queue = $container['queue'];
ContainerInterface
interface ContainerInterface { // inherited from Interop\Container\ContainerInterface public function get($id); // inherited from Interop\Container\ContainerInterface public function has($id); public function set($id, $value); public function remove($id); }
ContainerAwareInterface
Also provided is the ContainerAwareInterface, which allows your classes to declare that they support container injection. Nifty for granting objects access to your container (you weren't thinking of using a static class, were you?).
interface ContainerAwareInterface { public function setContainer(Minphp\Container\ContainerInterface $container = null); public function getContainer(); }
Usage
<?php namespace myApp\Controller; use Minphp\Container\ContainerAwareInterface; use Minphp\Container\ContainerInterface; class MyController implements ContainerAwareInterface { private $container = null; public function setContainer(ContainerInterface $container = null) { $this->container = $container; } public function getContainer() { return $this->container; } }
统计信息
- 总下载量: 957
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-12