Skip to content

Instantly share code, notes, and snippets.

@tomasfejfar
Created November 21, 2011 21:37
Show Gist options
  • Save tomasfejfar/1384029 to your computer and use it in GitHub Desktop.
Save tomasfejfar/1384029 to your computer and use it in GitHub Desktop.
DIC & DICProxy
<?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