- Notifications
You must be signed in to change notification settings - Fork1
Dependency Injection for PHP
License
NotificationsYou must be signed in to change notification settings
thiagodp/di
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Dependency Injection for PHP.
We usesemantic version. Seeour releases.
composer require phputil/di
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' );
interface MyInterface {functionsay($what );}class MyClassimplements MyInterface {functionsay($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' );
interface I {}class A {}class Bimplements 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' );
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
class A {}// Makes "A" a shared instanceDI::config(DI::let('A' )->shared() );$a1 =DI::create('A' );$a2 =DI::create('A' );var_dump($a1 ===$a2 );// bool(true)
interface I {}class Cimplements I {}// Makes "I" a shared instanceDI::config(DI::let('I' )->create('C' )->shared() );$i1 =DI::create('I' );$i2 =DI::create('I' );var_dump($i1 ===$i2 );// bool(true)
interface I {}class Cimplements I {}// Let you using a callable to create the desired instanceDI::config(DI::let('I' )->call(function() {returnnewC();} ) );$i =DI::create('I' );// Calls your function to create a "C" instance
class A {private$text;function__construct($text ) {$this->text =$text;}}// Lets you customize a callable with parametersDI::config(DI::let('A' )->call(function($a,$b ) {returnnewA($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
About
Dependency Injection for PHP
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.