项目作者: frostealth

项目描述 :
Simple Dependency Injection Container
高级语言: PHP
项目地址: git://github.com/frostealth/php-container.git
创建时间: 2014-08-29T17:27:09Z
项目社区:https://github.com/frostealth/php-container

开源协议:MIT License

下载


PHP Container

Simple Dependency Injection Container.

Installation

Run the Composer command to install the latest stable version:

  1. composer require frostealth/php-container @stable

Usage

  1. use frostealth\Container\Container;
  2. $container = new Container();
  3. // ...
  4. // injecting simple values
  5. $container->set('foo', 'bar'); // or $container->foo = 'bar';
  6. // get its value
  7. $value = $container->get('foo'); // or $value = $container->foo;
  8. // ...
  9. // resources
  10. $container->set('object', function ($container) {
  11. return new MyObject($container->foo);
  12. });
  13. // get a new instance
  14. $object = $container->get('object');
  15. // ...
  16. // singleton resources
  17. $container->singleton('log', function ($container) {
  18. return new MyLog($container->object);
  19. });
  20. // get log resource
  21. $log = $container->get('log');

Dependency Injection

  1. use Interop\Container\ContainerInterface;
  2. class MyClass
  3. {
  4. /**
  5. * @var ContainerInterface
  6. */
  7. protected $container;
  8. /**
  9. * @param ContainerInterface $container
  10. */
  11. public function __construct(ContainerInterface $container)
  12. {
  13. $this->container = $container;
  14. }
  15. }

License

The MIT License (MIT).
See LICENSE.md for more information.