Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Dependency Injection for PHP

License

NotificationsYou must be signed in to change notification settings

thiagodp/di

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dependency Injection for PHP.

Build Status

We usesemantic version. Seeour releases.

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 {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' );

Example 3: a more complex model

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' );

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 instanceDI::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 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)

Example 7: defining a creation function

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

Example 8: passing arguments for creation functions

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

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp