SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
-
Sep 11, 2005, 01:46 #1
- Join Date
- Jan 2005
- Posts
- 8
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
a few question of DI & ServiceLocator
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.
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');
}
}
PHP Code:function __construct(&$bar) {
/* long process ... */
$this->bar =& $bar;
}
Last edited by stdafx; Sep 11, 2005 at 03:12.
-
Sep 11, 2005, 03:44 #2
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You generally wouldn't have heavy processing code in a constructor. The purpose of the constructor is just to initiate the object.
Secondly - the Bar instance is needed no matter how you put it, so it doesn't matter when it's created. The only exception is if an error happens within the constructor, but this is problematic for other reasons aswell. Either way it's not something you would normally expect to happen.
-
Sep 11, 2005, 04:00 #3
- Join Date
- Nov 2001
- Location
- Bath, UK
- Posts
- 2,498
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by stdafx
hth,
DouglasHello World
-
Sep 11, 2005, 04:18 #4
- Join Date
- Jan 2005
- Posts
- 8
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for your answers.
but please excuse my slowness in understanding, Douglas. i'm a little confused of where the 'lazy loading' could took place. :'( since the constructor accepts an $bar object as parameter here, isn't the $bar instantiated before we call the constructor ?
-
Sep 11, 2005, 06:30 #5
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by stdafx
The problem with such a memory-hungry object doesn't get any worse with DI - the core problem was there even before.
-
Sep 11, 2005, 07:02 #6
- Join Date
- Jan 2005
- Posts
- 8
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
your explaining made me clear now. thank you very much, kyber.
-
Sep 11, 2005, 12:50 #7
Originally Posted by stdafx
-
Sep 12, 2005, 06:50 #8
- Join Date
- Apr 2003
- Location
- London
- Posts
- 2,423
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Hi...
Originally Posted by kyberfabrikken
The problem is that developers don't expect this behaviour when your language has traditionally not supported exceptions. For example, you expect a return value with an error code (ruling out the constructor). Also you get a half finished object anyway, so the gain is not obvious. With everything in the constructor you don't have to worry about lazy loading either, as the application will instantiate when it's ready.
However, I don't see any shift in viewpoint happening right now. This means that heavy constructors will currently confuse.
yours, MarcusMarcus Baker
Testing: SimpleTest, Cgreen, Fakemail
Other: Phemto dependency injector
Books: PHP in Action, 97 things
Bookmarks