PHP - Where is the best place to run a database class?

I recently took my Db initiation code from the __construct class of my class and placed it right after the start of the Page class. I removed it from the Page class because I want to be able to access it from anywhere (for example, for other classes). On initialization, it also takes server, username, password, and database arguments and I don't want to enter them every time.

Is there a way to access it from the Page class now? I tried several methods, even the global one (I was told this is a terrible way to do things) and so far nothing has worked. I'm still new to OO, but I teach myself as best I can.

Should I make it a static class? Will this affect the lazy socket on Db I have installed?

Any help would be much appreciated.

thank

[EDIT]

A similar question: Global or Singleton for database connection?

0


source to share


3 answers


A global type of some sort (be it globals, singleton, or whatever) is an improvement over your previous approach and therefore you are on the right track. Generally speaking, you should try to minimize the scope of the program (for a number of reasons that I won't go into). Having a global variable is contrary to this principle. There are various solutions to this problem, but the most powerful and often overlooked approach is using inversion of control; Instead of getting a dependency, your class should get it. For example, let's say you have this

class EditUserController {
  function saveUser() {
    $db = Database::GetInstance();
    $db->execute("update users set ...", ...);
  }
}

      

You can change this to:

class EditUserController {
  function saveUser($db) {
    $db->execute("update users set ...", ...);
  }
}

      



Passing dependencies from the functional parameter level can be a bit cumbersome, so the trade-off might be to pass it at the per-object level:

class EditUserController {
  protected $db;
  function __construct($db) {
    $this->db = $db;
  }
  function saveUser() {
    $this->db->execute("update users set ...", ...);
  }
}

      

This is a fairly common pattern in OO programming. In addition to being more practical than passing functional parameters, it has the added benefit of separating the construct (when common dependencies are connected to each other) from the runtime (where they are used). This simplifies a lot.

+2


source


Global variables are actually used and this will be one of them. Unless you probably need multiple database connections (or even else), then I see no problem setting up the global $ db object.

An alternative way is the "Factory" static class, which you can use to get an object. In Joomla 1.5, the way to access a DB object is as follows:

$db =& JFactory::getDBO();

      



the getDBO function checks if the DB object has been created: if there is one, return a reference to it, otherwise, connect and initialize, then return it.

This can equally apply to other "can-be-made-global" objects such as the current custom object.

0


source


The singleton method was created to ensure that only one instance of any class exists. But because people use it as a way to speed up the globalization process, it becomes known as lazy and / or bad programming.

0


source







All Articles