Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8673823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:33:28+00:00 2026-06-12T19:33:28+00:00

I have a large amount of classes for: a main forums class, a forum

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:33:29+00:00Added an answer on June 12, 2026 at 7:33 pm

    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.

    ex: $threads = new ThreadClass($mysqlConnection);
    

    I hope this helps

    # 1. mysql source or "socket" using PDO
    class SimpleDatabaseSocket {
    
        protected static $dbh = false;
    
        public function connect($database, $user, $pass, $host) {
            $dsn = "mysql:dbname={$database};host={$host}";
            self::$dbh = new PDO($dsn, $user, $pass);
            self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
    
    }
    
    # 2. always use an interface which defines required logic
    interface SimpleDatabaseInterface {
    
        public function onRunQuery($query);
    }
    
    # 3. create the accessible controller for inherited & ancillary logic
    class SimpleDatabaseController extends SimpleDatabaseSocket implements SimpleDatabaseInterface {
    
        private $host = "";
        private $base = "";
        private $usr = "";
        private $pwd = "";
        private $table = "";
    
        # when we create the controller, you MUST pass the connection params
        public function __construct($host, $usr, $pwd, $base) {
            $this->setUsr($usr);
            $this->setPwd($pwd);
            $this->setBase($base);
            $this->setHost($host);
        }
    
        # mysql host/ip
        public function setHost($host) {
            $this->host = $host;
        }
    
        # mysql database
        public function setBase($base) {
            $this->base = $base;
        }
    
        # mysql username
        public function setUsr($usr) {
            $this->usr = $usr;
        }
    
        # mysql password
        public function setPwd($pwd) {
            $this->pwd = $pwd;
        }
        # allow this controller to switch tables dynamically.
        public function onChangeTable($table) {
            $this->table = $table;
        }
    
        # connect to the database
        protected function onConnect() {
            $this->connect($this->base, $this->usr, $this->pwd, $this->host);
        }
    
        # connects to the DB and runs a custom query
        public function onRunQuery($query) {
            try {
                if (!self::$dbh)
                    $this->onConnect();
                $result = self::$dbh->query($query);
                $out = $result->fetchAll(PDO::FETCH_ASSOC);
            } catch (PDOException $e) {
    
            }
            return $out;
        }
    
    }
    
    # exmaple usage
    $mysql = new SimpleDatabaseController("HOSTNAME","USERNAME","PASSWORD","DATABASE");
    $result = $mysql->onRunQuery("SELECT * FROM TABLE LIMIT 0,2");
    print("<pre>" . print_r($result, true) . "</pre>");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large amount of data to sort and query, and I can't
I have a WCF service that can return large amount of data depending on
I have a VB6 EXE project with a large amount of classes - everything
I have a large amount of data in a database. When I attempt to
I have a program in which I have a large amount of items listed
I need some help in solving this problem. We have a large amount of
I have a rather large amount of data (100 MB or so), that I
I have a table with large amount of data and want to take too
I have to send a large amount of xml data through sockets. Example of
I have a highly formatted file with large amount of data which I used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.