Skip to content

Instantly share code, notes, and snippets.

@borekb
Created March 13, 2012 00:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borekb/2025561 to your computer and use it in GitHub Desktop.
Save borekb/2025561 to your computer and use it in GitHub Desktop.
Simplest
<?php
// třída, funkce, proměnná, prostě cokoliv, co poskytuje něco,
// co chce někdo jiný konzumovat
class Something {
function doWork() {
echo "some work...";
}
}
// konzument bez DI - ke třídě, na které je závislý, se dopracovává sám
function withoutDI() {
$something = new Something();
$something->doWork();
}
// konzument s DI - nechá si závislost předat zvenku
function withDI(Something $something) {
$something->doWork();
}
// jak se použijí obě implementace:
withoutDI();
withDI(IoCContainer::getInstance(Something));
// příklad IoC kontejneru
class IoCContainer {
static function getInstance($class) {
return new $class;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment