pe/component-simple-di

此包的最新版本(v1.0.1)没有可用的许可证信息。

简单的依赖注入容器组件

v1.0.1 2018-10-14 12:48 UTC

This package is auto-updated.

Last update: 2024-09-15 02:04:04 UTC


README

要求

以下版本的 PHP 被支持。

  • PHP 5.5+

安装

要安装,请使用 composer

php composer.phar require pe/component-simple-di

使用方法

<?php
use PE\Component\SimpleDI\Container;
use PE\Component\SimpleDI\ServiceProviderInterface;

// Instantiate container
$di = new Container();

// Add simple value, can be any value
$di->set('foo', 'bar');
$di->get('foo'); //-> just return 'bar'

// Add service definition (closure factory)
$di->set('foo', $di->service(function () {
    return new \stdClass();
}));

$di->get('foo'); //-> call instantiator function and returns instance
$di->get('foo'); //-> returns instance same as in previous call

// Also you can register services via provider
// A. Create provider class
class SomeServiceProvider implements ServiceProviderInterface
{
    public function register(Container $container)
    {
        $container->set('foo', $container->service(function () {
            return new \stdClass();
        }));
    }    
}

// B. Register provider
$di->register(new SomeServiceProvider());