Created
September 15, 2012 00:39
-
-
Save asaokamei/3725826 to your computer and use it in GitHub Desktop.
Automatic Dependency Injection using Interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Example of simple automatic dependency injection using only interface. | |
this code is equivalent to: | |
$service = new \MyService\Service; | |
$invoice = new \Application\Invoice; | |
$invoice->injectService( $service ); | |
$invoice->doIt(); | |
*/ | |
namespace MyService { | |
interface injectServiceInterface {} | |
class Service { | |
public function doIt() { | |
return 'done that.'; | |
} | |
} | |
} | |
namespace Application { | |
class Invoice implements \MyService\injectServiceInterface | |
{ | |
/** @var \MyService\Service */ | |
var $service = NULL; | |
function injectService( $service ) { | |
$this->service = $service; | |
} | |
function doIt() { | |
return $this->service->doIt(); | |
} | |
} | |
} | |
namespace main | |
{ | |
$invoice = new \Application\Invoice(); | |
inject( $invoice ); | |
echo $invoice->doIt(); // shows 'done that.' | |
function inject( $object ) | |
{ | |
if( !$interfaces = class_implements( $object ) ) return; | |
foreach( $interfaces as $interface ) // get a list of interfaces. | |
{ | |
if( preg_match( '/^(.*)Inject([_a-zA-Z0-9]+)Interface$/i', | |
$interface, $matches ) ) | |
{ | |
$className = $matches[1] . $matches[2]; // class to inject. | |
$injector = "inject" . $matches[2]; // method to inject. | |
$injObj = new $className; // create an object. | |
$object->$injector( $injObj ); // and inject it! | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple automatic dependency injection for PHP.
This is a conceptual code that works >= PHP5.3 which automatically inject dependent object using only interface.