Dependency Injection

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Dependency Injection

Post by Chris Corbyn »

I'd always assumed DI was some really complex design pattern, yet it appears I've been using DI without even knowing it, unless I'm missing something.

Apologies for the link to a Java article, it just happens to be what I was reading:

http://www.javaranch.com/journal/200709 ... 709.jsp#a4

Is that seriously all there is to it? Injecting objects which implement an interface via a setter method? I could have sworn I'd seen more complicated demonstrations of it than that. Hmm...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Passing in the "dependency" itself in the constructor is only *one* of many ways to Inject dependencies ;)

(eg: If you passed in a ResourceLocator in the constructor you would immediately be able to get all dependencies when you need them...)

And then the fun begins with Spring Injection etc because they provide whole libraries to not only inject dependencies, but also load factories that are able to create the actual objects (on which the consumers depend))...

Anyway, as i already mentionned, an article summing up a couple of solutions to solve the problem: http://martinfowler.com/articles/injection.html
Last edited by timvw on Fri Sep 14, 2007 6:07 am, edited 2 times in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

timvw wrote:Passing in the "dependency" itself in the constructor is only *one* of many ways to Inject dependencies ;)

(eg: If you passed in a ResourceLocator in the constructor you would immediately be able to get all dependencies when you need them...)
Hmm... I'll have to look into this again then :) Everybody does talk about the spring framework (or Phemto in PHP) when they refer to DI. Neither of those am I familiar with.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ah ha, I got it. I was thinking factory classes would be a good way to implement DI. Looking at a Spring configuration excerpt you see this:

Code: Select all

<beans>
	<bean id="AirlineAgency" class="com.dnene.ditutorial.common.impl.SimpleAirlineAgency" singleton="true"/>
	<bean id="CabAgency" class="com.dnene.ditutorial.common.impl.SetterBasedCabAgency" singleton="true">
		<property name="airlineAgency">
			<ref bean="AirlineAgency"/>
		</property>
	</bean>
	<bean id="TripPlanner" class="com.dnene.ditutorial.common.impl.SetterBasedTripPlanner" singleton="true">
		<property name="airlineAgency">
			<ref bean="AirlineAgency"/>
		</property>
		<property name="cabAgency">
			<ref bean="CabAgency"/>
		</property>
	</bean>
</beans>
So basically the dependencies are listed in XML and I can jump to a fairly confident conclusion that they have some generic factory which glues everything together. Pretty neat I guess :)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Have a look at Pico Container. There's a PHP implementation available.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

It's pretty neat, but use it with sense.. Otherwise you'll experience "Great Pain(tm)" when you transition from DVL to TST/QUA/PRD and have to figure out where you injected mocks... ;)
Post Reply