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?
For instance, right now I'm having to use a global, unfortunately:
class Foo {
public static function Bar() {
global $DB;
return $DB->DSN_STRING;
}
}
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.
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.
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?
The thing most of all is that I avoid calling "global $DB" because people freak out about that.