I have a large amount of classes for: a main forums class, a forum boards class, and a threads class. These can be thought of as models in the context of my website.
The setup the previous web developer was using creates a new mysqli connection in each one of these classes and connects on instantiation of the class. This means that in the threads page there are 3 different connections for forums, threads, and users. This is obviously not ideal and I think not even optimal.
I worked on a previous project in which I could simply just pass a new instance of the database class to the class I was using, but this isn’t ideal because I need to instantiate multiple classes and passing a database instance to each class would defeat the purpose.
In each of these classes, database calls are made so the database object/instance is needed within each class, without having to instantiate it 3+ separate times.
For example, in the threads class you could have:
function get_threads($board_id) {
return $this->con->query("some query");
}
Does anyone know how I can go about achieving this?
I prefer one database controller that you could get data from as in a factory pattern. In the example below, you could just create one controller and let your components use it as they need.
I hope this helps