Created
March 13, 2012 00:02
-
-
Save borekb/2025561 to your computer and use it in GitHub Desktop.
Simplest
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 | |
// 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