from kyber's post:
http://www.sitepoint.com/forums/show...9&postcount=31
so, in this case DI seems better than ServiceLocator because ServiceLocator depends on more things.
but also as i learned from the code above, when using ServiceLocator, we can take control of when to create the $bar object.
the object $bar is only instantiated at the time we just need it. however, when using DI, the constructor may implement like this:PHP Code:class SomeComponent {
private $bar;
function __construct(&locator) {
/* some codes
lead to long process here ... */
// finally we create the $bar ^^
$this->bar =& $locator->createInstance('Bar');
}
}
it looks like the $bar is required to be instantiated even before the constructor works. suppose $bar is a very large object contains lots of data, isn't it wasting memories ?PHP Code:function __construct(&$bar) {
/* long process ... */
$this->bar =& $bar;
}





Bookmarks