Created
November 21, 2011 21:37
-
-
Save tomasfejfar/1384029 to your computer and use it in GitHub Desktop.
DIC & DICProxy
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 | |
class DICProxy | |
{ | |
public function __construct($dic, $className) { | |
//set to private members | |
} | |
public function __call($method, $args) | |
{ | |
return $dic->giveMe($className)->$method($args); //super-simplified | |
} | |
} | |
//usage | |
$dbModel = $dic->giveMe('MyModel'); | |
//what DIC does behind the scenes: | |
if (exist('MyModel') && $allowSingleton) { | |
return $this->reuseInstance('MyModel'); | |
} | |
$instance = new MyModel(); | |
$instance->setDbAdapter(new DICProxy('DbAdapter')); | |
$instance->setAuthAdapter(new DICProxy('AuthAdapter')); | |
//...maybe cache somewhere for later use as singleton | |
//when you call | |
$instance->getDbAdapter()->query() | |
// the DICProxy instantiates calls DIC to receive the DbADapter using giveMe() | |
// dependency is instantiated at last possible moment |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment