I have a database class which is constructed so that it opens a connection to the database when the first query is run. Should each of the other classes, used for the site's users, groups and so on, create one database object each? This makes the classes easy to deploy elsewhere; they will only depend on the database class. This will however need me to put the credentials in each class. Another approach which seems less messy for me is create a database object and keep it in a global variable. This reduces the active database connections at the same time as it makes it easier to config; the credentials only have to be set once.
Somewhere I've heard that global variables should be avoided. What's the best practice approach to this?