Dependence Injection in PHP
Moderator: General Moderators
Dependence Injection in PHP
Looking for an explanation of Dependency Injection and perhaps an example of it in PHP. If anybody knows of a good article, I'd be appreciative.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Quick google search states that this also applies to php..
http://www.martinfowler.com/articles/injection.html
http://www.martinfowler.com/articles/injection.html
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
That is perhaps the definitive article, but remember that Inversion of Control and Dependency Injection are patterns so there are not hard and fast implementations. An additional problem is that PHP being see as the land of the "design conservatives" does not have very much written about subjects like this. And that's a problem because Dependency Injection looks different in PHP than Java or .NET where it is done in the specific style of those languages.
There are also some other issues, such as persistence, that touch upon this subject once you get into it. However, in its most basic form in PHP it is about being able to get fully instantiated objects without having to know how to do fully instantiate them.
There are also some other issues, such as persistence, that touch upon this subject once you get into it. However, in its most basic form in PHP it is about being able to get fully instantiated objects without having to know how to do fully instantiate them.
(#10850)
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
Shameless thread necromancy...
I've recently been reading up and trying to gain a better understanding of Dependancy Injection, and it appears I had the wrong grasp of it.
As Martin Fowler says in his artcle, he referred to DI as a 'Plugin' system in PoEAA, and that there are 3 methods if DI - Constructor Injection, Setter Injection and Interface Injection (the 3rd is not easy to, or infact impossible to do in PHP I think.. my example below is of the Setter Injection - if I have the right idea :p)
From my very brief understanding of it all, in it's purest form - it comes down to type hinting your arguments...
FooBar is dependant on the classes that implement the iSomeInterface interface, because it requires the use of the someMethod() method.
Another example where the name of the class to use is passed as string:
Now this, if I am right, was some what of a revelation to me.. it's one hell of a lot simpler than I thought it would be.
I've recently been reading up and trying to gain a better understanding of Dependancy Injection, and it appears I had the wrong grasp of it.
As Martin Fowler says in his artcle, he referred to DI as a 'Plugin' system in PoEAA, and that there are 3 methods if DI - Constructor Injection, Setter Injection and Interface Injection (the 3rd is not easy to, or infact impossible to do in PHP I think.. my example below is of the Setter Injection - if I have the right idea :p)
From my very brief understanding of it all, in it's purest form - it comes down to type hinting your arguments...
Code: Select all
<?php
interface iSomeInterface
{
public function someMethod();
}
class FooBar
{
private $foo;
public function setFoo(iSomeInterface $dependancy)
{
$this->foo = $dependancy;
$this->foo->someMethod();
}
}
?>Another example where the name of the class to use is passed as string:
Code: Select all
<?php
class FooBar
{
private $foo;
public function __construct($depend)
{
if (($foo = new $depend) instanceof SomeClass) // can also use (get_class($foo) == 'SomeClass')
{
$this->foo = $foo;
}
else
{
throw new InvalidArgumentException('Dependancy ' . $depend . ' must be instance of SomeClass');
}
}
}
?>- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
Jenk: you only got one part of DI - the Dependency part. Now you need Injection. Injection is the part that creates the needed dependancy object and sets it (or passes it to constructor).
As Maugrim suggested, head over to sitepoint and search there. Quite a few threads on the topic there. Also, you can search for Phemtoo, which is a PHP's little brother of PicoContainer (Java DI).
As Maugrim suggested, head over to sitepoint and search there. Quite a few threads on the topic there. Also, you can search for Phemtoo, which is a PHP's little brother of PicoContainer (Java DI).
I've been searching and searching on sitepoint, thus far only finding references to either Martin Fowler's article, Picocontainer (which refers to Martin Fowler's article) or Phemto which doesn't have anything but source code (and a lot of it)
This is where I will appear ultimately thick, but I have real problems reading text like Martin Fowler's article. No fault of Mr Fowler. I forget really easily.. like I'll forget what was said at the beginning of a sentence before finishing it.. maybe I have undiagnosed dyslexia/adhd or something, I don't know.
So can anyone please post an uber, uber dumbed down version of dependency injection description?
It's taken me from the time the MF article was posted by astions to get as far as I did above :\
This is where I will appear ultimately thick, but I have real problems reading text like Martin Fowler's article. No fault of Mr Fowler. I forget really easily.. like I'll forget what was said at the beginning of a sentence before finishing it.. maybe I have undiagnosed dyslexia/adhd or something, I don't know.
So can anyone please post an uber, uber dumbed down version of dependency injection description?
It's taken me from the time the MF article was posted by astions to get as far as I did above :\
Last edited by Jenk on Tue Sep 12, 2006 8:44 pm, edited 1 time in total.
Nevermind the above post..
Thought to myself "instead of trying to understand how others did/do it.. give it a go yourself":
viewtopic.php?p=309081#309081
Thought to myself "instead of trying to understand how others did/do it.. give it a go yourself":
viewtopic.php?p=309081#309081
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
A few of the Sitepoint threads may also be found by searching for Service Locator...
http://www.sitepoint.com/forums/search. ... id=2465163 (in case searching wrong forum...)
http://www.sitepoint.com/forums/search. ... id=2465163 (in case searching wrong forum...)