fabs/di

This package is abandoned and no longer maintained. No replacement package was suggested.

Dependency Injection for PHP

v1.03 2018-02-20 09:29 UTC

This package is not auto-updated.

Last update: 2020-01-16 20:08:21 UTC


README

Library that provides ways to use DI for PHP developers.

Install

Install the library like this composer require fabs/di

Quick Start

Set a service with lazy loading

$di = DI::getDefault();

$di->set('function_example',function(){
    return new MyCustomService();
});
// or
$di->set('class_name_example', MyCustomService::class);
// or
$di->set('parameter_example',function($first,$second){
    return new MyCustomService($first, $second);
})->setParameters([1,'second']);

Set a service without lazy loading

$di = DI::getDefault();

$di->set('test_service', new MyCustomService());

Set a shared service

$di = DI::getDefault();

$di->setShared('test_service', new MyCustomService());
// or
$di->set('test_service', new MyCustomService(), true);

Get a service

$di = DI::getDefault();

$service = $di->get('test_service');
// or
$service = $di['test_service'];
// or
$service = $di->get('parameter_example',[8,'example']);