I'm new to using MySQLi. I've used PHP's mysql_*
functions forever and am far too comfortable with them, but I write object oriented code and thought it's time to start using a more powerful and flexible database approach.
I've set up a class DB
which has a static method (DB::get()
) for returning (or building and returning) my connection, which is a static variable within the class.
What I want to know is how to use this in my other classes. I see a lot of people creating a local class variable ($this->db
or otherwise) in each class that will use the connection, which is initialised in the __construct()
function. I'm perfectly happy with that, but what about static methods?
Because there's no object, the __construct()
function hasn't been called. Is it simply a case of calling:
$db = DB::get();
In each static method? This seems a little clunky, especially considering the mysql_*
functions don't require any of this.