How can I use "Dependency Injection" in simple php functions, and should I bother?

Posted by Tchalvak on Stack Overflow See other posts from Stack Overflow or by Tchalvak
Published on 2010-02-12T23:22:13Z Indexed on 2010/03/31 14:23 UTC
Read the original article Hit count: 338

I hear people talking about dependency injection and the benefit of it all the time, but I don't really understand it.

I'm wondering if it's a solution to the "I pass database connections as arguments all the time" problem.

I tried reading wikipedia's entry on it, but the example is written in Java so I don't solidly understand the difference it is trying to make clear. ( http://en.wikipedia.org/wiki/Dependency_injection ).

I read this dependency-injection-in-php article ( http://www.potstuck.com/2009/01/08/php-dependency-injection/ ), and it seems like the objective is to not pass dependencies to an object directly, but to cordon off the creation of an object along with the creation of it's dependencies. I'm not sure how to apply that in a using php functions context, though.

Additionally, is the following Dependency Injection, and should I bother trying to do dependency injection in a functional context?

Version 1: (the kind of code that I create, but don't like, every day)

function get_data_from_database($database_connection){
    $data = $database_connection->query('blah');
    return $data;
}

Version 2: (don't have to pass a database connection, but perhaps not dependency injection?)

function get_database_connection(){
    static $db_connection;
    if($db_connection){
        return $db_connection;
    } else {
        // create db_connection
      ...
    }
}

function get_data_from_database(){
   $conn = get_database_connection();
   $data = $conn->query('blah');
   return $data;
}

$data = get_data_from_database();

Version 3: (the creation of the "object"/data is separate, and the database code is still, so perhaps this would count as dependency injection?)

function factory_of_data_set(){
    static $db_connection;
    $data_set = null;
    $db_connection = get_database_connection();
    $data_set = $db_connection->query('blah');
    return $data_set;
}

$data = factory_of_data_set();

Anyone have a good resource or just insight that makes the method and benefit -crystal- clear?

© Stack Overflow or respective owner

Related posts about idiomatic

Related posts about dependency-injection