2017 © Pedro Peláez
 

library di

Dependency Injection for PHP

image

phputil/di

Dependency Injection for PHP

  • Tuesday, February 13, 2018
  • by thiagodp
  • Repository
  • 4 Watchers
  • 6 Stars
  • 112 Installations
  • PHP
  • 0 Dependents
  • 1 Suggesters
  • 2 Forks
  • 0 Open issues
  • 10 Versions
  • 12 % Grown

The README.md

di

Dependency Injection for PHP., (*1)

Build Status, (*2)

We use semantic version. See our releases., (*3)

Classes

Installation

composer require phputil/di

Example 1: automatic injection without configuring anything

class A {}
class B {}

class C {
    public $a, $b;
    function __construct( A $a, B $b ) {
        $this->a = $a;
        $this->b = $b;
    }
}

// Automatically creates "A" and "B", and inject them in "C"
$c = DI::create( 'C' );

Example 2: configuring the injection for interfaces

interface MyInterface {
    function say( $what );
}

class MyClass implements MyInterface {
    function say( $what ) { echo $what; }
}

// Configures "MyInterface" to be created using "MyClass"
DI::config( DI::let( 'MyInterface' )->create( 'MyClass' ) );

$foo = DI::create( 'MyClass' ); // Create an instance of MyClass (no configuration required)
$foo->say( 'hello' );

$bar = DI::create( 'MyInterface' ); // Create an instance of MyClass!
$bar->say( 'world' );

Example 3: a more complex model

interface I {}

class A {}

class B implements I {}

class C {
    public $i;
    function __construct( I $i ) {
        $this->i = $i;
    }
}

class X {
    public $a, $c;
    function __construct( A $a, C $c ) {
        $this->a = $a;
        $this->c = $c;
    }
}

// Configures "I" to be created using "B"
DI::config( DI::let( 'I' )->create( 'B' ) );

// Automatically creates and injects "A" and "C", and
// when creates "C", also injects "B" as "I".
$x = DI::create( 'X' );

Example 4: passing constructor's arguments

class A {
    function __construct( $one, $two = 'world' ) {
        echo $one, ', ', $two;
    }
}

// Creates "A", passing constructor arguments as an array
$a = DI::create( 'A', array( 'hello' ) ); // prints hello, world

Example 5: configuring shared instances

class A {}

// Makes "A" a shared instance
DI::config( DI::let( 'A' )->shared() );

$a1 = DI::create( 'A' );
$a2 = DI::create( 'A' );
var_dump( $a1 === $a2 ); // bool(true)

Example 6: configuring shared instances for interfaces

interface I {}

class C implements I {}

// Makes "I" a shared instance
DI::config( DI::let( 'I' )->create( 'C' )->shared() );

$i1 = DI::create( 'I' );
$i2 = DI::create( 'I' );
var_dump( $i1 === $i2 ); // bool(true)

Example 7: defining a creation function

interface I {}

class C implements I {}

// Let you using a callable to create the desired instance
DI::config( DI::let( 'I' )->call( function() {
        return new C();
    } ) );

$i = DI::create( 'I' ); // Calls your function to create a "C" instance

Example 8: passing arguments for creation functions

class A {
    private $text;
    function __construct( $text ) {
        $this->text = $text;
    }
}

// Lets you customize a callable with parameters
DI::config( DI::let( 'A' )->call( function( $a, $b ) {
        return new A( $a . $b );
    } ) );

// Uses the customized constructor
$a = DI::create( 'A', array( 'Hello, ', 'world' ) );
echo $a->text(); // Hello, world

// Ignore the customized constructor passing true after the parameters
$a2 = DI::create( 'A', array( 'Hi!' ), true );
echo $a2->text(); // Hi

The Versions

13/02 2018

dev-master

9999999-dev http://github.com/thiagodp/di

Dependency Injection for PHP

  Sources   Download

LGPL-3 LGPL-3.0-or-later

The Requires

  • php >=5.4

 

The Development Requires

dependency injection injector

01/03 2017

0.3.1

0.3.1.0 http://github.com/thiagodp/di

Dependency Injection for PHP

  Sources   Download

LGPL-3

The Requires

  • php >=5.4

 

The Development Requires

dependency injection injector

07/07 2016

0.3

0.3.0.0 http://github.com/thiagodp/di

Dependency Injection for PHP

  Sources   Download

LGPL-3

The Requires

  • php >=5.4

 

The Development Requires

dependency injection injector

05/06 2016

0.2.4

0.2.4.0 http://github.com/thiagodp/di

Dependency Injection for PHP

  Sources   Download

LGPL-3

The Requires

  • php >=5.4

 

The Development Requires

dependency injection injector

23/04 2016

0.2.3

0.2.3.0 http://github.com/thiagodp/di

Dependency Injection for PHP

  Sources   Download

LGPL-3

The Requires

  • php >=5.4

 

The Development Requires

dependency injection injector

13/11 2015

0.2.1

0.2.1.0 http://github.com/thiagodp/di

Dependency Injection for PHP

  Sources   Download

LGPL-3

The Requires

  • php >=5.4

 

The Development Requires

dependency injection injector

13/11 2015

0.2.2

0.2.2.0 http://github.com/thiagodp/di

Dependency Injection for PHP

  Sources   Download

LGPL-3

The Requires

  • php >=5.4

 

The Development Requires

dependency injection injector

13/11 2015

0.2

0.2.0.0 http://github.com/thiagodp/di

Dependency Injection for PHP

  Sources   Download

LGPL-3

The Requires

  • php >=5.4

 

The Development Requires

dependency injection injector

12/11 2015

0.1.1

0.1.1.0 http://github.com/thiagodp/di

Dependency Injection for PHP

  Sources   Download

LGPL-3

The Requires

  • php >=5.4

 

The Development Requires

dependency injection injector

12/11 2015

0.1

0.1.0.0 http://github.com/thiagodp/di

Dependency Injection for PHP

  Sources   Download

LGPL-3

The Requires

  • php >=5.4

 

The Development Requires

dependency injection injector