title: How can I use "Dependency Injection" in simple php functions, and should I bother?
body: <p>I hear people talking about dependency injection and the benefit of it all the time, but I don't really understand it.</p>
  
  <p>I'm wondering if it's a solution to the "I pass database connections as arguments all the time" problem.</p>
  
  <p>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. ( <a href="http://en.wikipedia.org/wiki/Dependency_injection" rel="noreferrer">http://en.wikipedia.org/wiki/Dependency_injection</a> ).</p>
  
  <p>I read this dependency-injection-in-php article ( <a href="http://www.potstuck.com/2009/01/08/php-dependency-injection/" rel="noreferrer">http://www.potstuck.com/2009/01/08/php-dependency-injection/</a> ), 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.</p>
  
  <p>Additionally, is the following Dependency Injection, and should I bother trying to do dependency injection in a functional context?</p>
  
  <p>Version 1: (the kind of code that I create, but don't like, every day)</p>
  
  <pre><code>function get_data_from_database($database_connection){
      $data = $database_connection-&gt;query('blah');
      return $data;
  }
  </code></pre>
  
  <p>Version 2: (don't have to pass a database connection, but perhaps not dependency injection?)</p>
  
  <pre><code>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-&gt;query('blah');
     return $data;
  }
  
  $data = get_data_from_database();
  </code></pre>
  
  <p>Version 3: (the creation of the "object"/data is separate, and the database code is still, so perhaps this would count as dependency injection?)</p>
  
  <pre><code>function factory_of_data_set(){
      static $db_connection;
      $data_set = null;
      $db_connection = get_database_connection();
      $data_set = $db_connection-&gt;query('blah');
      return $data_set;
  }
  
  $data = factory_of_data_set();
  </code></pre>
  
  <p>Anyone have a good resource or just insight that makes the method and benefit -crystal- clear?</p>