title: PHP: How to pass existing database connection to static class methods?
body: <p>I have a set of static class methods. I also have an existing database connection in a script stored in an object variable $DB. How do I call those static class methods and let them use that $DB object without having to pass them this variable every time as a parameter on the class method?</p>
  
  <p>For instance, right now I'm having to use a global, unfortunately:</p>
  
  <pre><code>class Foo {
    public static function Bar() {
      global $DB;
      return $DB-&gt;DSN_STRING;
    }
  }
  </code></pre>
  
  <p>It's like I need my static class to call itself with a routine that somehow gets the $DB connection without having to re-establish it. Note I can't inject it into the static class because it's not instantiated.</p>
  
  <p>Of course the problem is solved if I switch from a static class to a regular class and instantiate my $Foo object. I could then inject the $DB var as a setting on a public variable. Or add a public method to receive the $DB var and then set a private var of the $Foo object. Or, make a class constructor accept the $DB var and set a private var of the $Foo object. But all 3 techniques require that I switch from a static class to a regular class.</p>
  
  <p>Some people have mentioned something called a Registry pattern or a Singleton pattern (I think it's the same thing? Not sure). Is this what I need to resolve this problem efficiently?</p>
  
  <p>The thing most of all is that I avoid calling "global $DB" because people freak out about that.</p>