Dependence Injection in PHP

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
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Dependence Injection in PHP

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I'm watching 8)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Quick google search states that this also applies to php..

http://www.martinfowler.com/articles/injection.html
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

"inescapable" :D I find it's the only way I understand!
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post 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).
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :\
Last edited by Jenk on Tue Sep 12, 2006 8:44 pm, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...)
Post Reply