2017 © Pedro Peláez
 

library dic

Dependency Injection Container.

image

pkj/dic

Dependency Injection Container.

  • Friday, November 28, 2014
  • by peec
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

PHP - Dependency Injection Container

Simple dependency injection container (DIC) or otherwise known as Inversion of Control (IoC). DIC helps you sort out dependencies for objects in a nice manner., (*1)

Example



$di = new DependencyInjector(); class World { private $str; public function __construct($str) { $this->str = $str; } public function __toString () { return $this->str; } } $di->service('world', function ($string) { return new World($string); }, ['World']); $di->service('test', function ($number, $thing) { return "Hello $thing, this is a number: $number!"; }, [1337, '@world']); echo $di->service('test');

Outputs:, (*2)

Hello World, this is a number: 1337!

Documentation

Services

Use the service method to create new services., (*3)

  • Argument 1: The name of the service
  • Argument 2: A callable that should return the value of the service when we want to fetch your service. Can return anything from a primitive value to a complex object.
  • Argument 3: Values to pass into the Argument 2's arguments. Use a string starting with @ to inject a service, otherwise you can inject anything. Note that everything starting with a @ is treated as a service.

Configuration

It's possible to configure the DependencyInjector with the config method. You can also define custom global settings using ->config($key, $value)., (*4)

Listed configuration below:, (*5)

DependencyInjector::CONF_STATIC_ANALYSIS

For convenience you can set DependencyInjector::CONF_STATIC_ANALYSIS to true, by doing this you don't need to specify arguments in the third argument using the service method., (*6)

Example:, (*7)

// Enable static anlysis using reflection.
$di->config(DependencyInjector::CONF_STATIC_ANALYSIS, true);

// The following code:


$di->service('test', function ($world) {
    return "Hello $world!";
}, ['@world']);


// Can now become:

$di->service('test', function ($world) {
    return "Hello $world!";
});


The Versions

28/11 2014

dev-master

9999999-dev http://github.com/peec/dependencyinjector

Dependency Injection Container.

  Sources   Download

MIT

The Requires

  • php >=5.4.0