Page 1 of 1

Dependence Injection in PHP

Posted: Wed Aug 09, 2006 1:23 am
by Luke
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.

Posted: Wed Aug 09, 2006 2:34 am
by Chris Corbyn
I'm watching 8)

Posted: Wed Aug 09, 2006 2:39 am
by Benjamin
Quick google search states that this also applies to php..

http://www.martinfowler.com/articles/injection.html

Posted: Wed Aug 09, 2006 1:27 pm
by Christopher
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.

Posted: Wed Aug 09, 2006 2:15 pm
by Luke
Arborint: I have not yet read the Martin Fowler article, but I plan to once I get the time... but in the mean time... have you actually seen it implemented in PHP? Maybe we could work on it like we did with the Service Locator?

Posted: Wed Aug 16, 2006 11:11 pm
by daedalus__
I thought that Dependancy Injection == Service Locator?

I do not understand either of them yet but that is what I have read?

EDIT: Nevermind. Though it would be nice for someone to explain the difference or how they work together? What they are too.. lol

Posted: Tue Sep 12, 2006 10:32 am
by Jenk
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...

Code: Select all

<?php

interface iSomeInterface
{
    public function someMethod();
}

class FooBar
{
    private $foo;

    public function setFoo(iSomeInterface $dependancy)
    {
        $this->foo = $dependancy;
        $this->foo->someMethod();
    }
}

?>
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:

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');
        }
    }
}

?>
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.

Posted: Tue Sep 12, 2006 11:19 am
by Maugrim_The_Reaper
There is a ton of reading material (forum posts) on the Sitepoint PHP Forums - quick search will throw up the relevant threads. This also links to one or two PHP DI libraries should the need to delve into actual code prove inescapable...

Posted: Tue Sep 12, 2006 11:22 am
by Jenk
"inescapable" :D I find it's the only way I understand!

Posted: Tue Sep 12, 2006 2:39 pm
by dbevfat
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).

Posted: Tue Sep 12, 2006 6:07 pm
by Jenk
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 :\

Posted: Tue Sep 12, 2006 8:21 pm
by Jenk
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

Posted: Wed Sep 13, 2006 3:02 am
by Maugrim_The_Reaper
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...)