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 6746351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:18:44+00:00 2026-05-26T12:18:44+00:00

I’m a little bit stuck with the following problem: I would like all my

  • 0

I’m a little bit stuck with the following problem: I would like all my classes to just use 1 single mysqli connection. I don’t particulary like dependency injection (it seems unelegant to me to pass the connection around via the constructor) so I implemented a singleton. This works well except for the following problem:

class Admin {
  private $DB;

  public function __construct() {
    $this->DB = new DB::get_instance();
  }

  public function get_all_users() {
    $this->DB->query('SELECT `email` FROM `users`');
    while ($row = $this->DB->result->fetch_row()) { $users[] = new User($row[0]); }
    return $users;
  }
}

class User {
  private $DB;

  public function __construct($email = FALSE){
    $this->DB = new DB;
    if ($email) {
      $this->load($email);
    }
  }

  public function load($email){
    $this->DB->query('SELECT * FROM `users` WHERE email = "'.$this->email.'";'); // Problem!
    // etc.
}

This doesn’t work as expected (=Returns just one user instead of all of them) as the query() in User overwrites the “mysqli_result var” from Admin with the new query (which obviously makes sense as there is only one instance of DB since it’s a singleton). So due to this nesting of queries a singleton won’t work.

What I would like to do now is to store the connection in a separate singleton class and create new DB classes for querying etc. on the go (which would use the connection from the singleton class). Basically something like this:

class DB extendes Connection { .... } // Called as $DB = new DB in other classes
class Connection extends mysqli { .... } // This is a singleton

But I just can’t figure it out. If I call parent::__construct() from DB it will create a new Connection instance which obviously isn’t what I’m looking for. Cloning Connection obviously won’t work either. How can I tell DB to use the mysqli link from Connection without actually creating a new Connection object?

I hope I layed out my problem more or less clearly 🙂 As mentioned above I’m somewhat stuck and I didn’t find any helpfull hints so far

  • 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-05-26T12:18:44+00:00Added an answer on May 26, 2026 at 12:18 pm
    <?php 
    //Create a registry class to hold objects that traverse all the classes
    Class Registry {
        private $vars = array();
        public function __set($index, $value){$this->vars[$index] = $value;}
        public function __get($index){return $this->vars[$index];}
    }
    
    //Your singleton db class
    class db{
        private static $instance = NULL;
        private function __construct() {
        }
        public static function getInstance($DBHOST,$DBDB,$DBUSER,$DBPASS) {
            if (!self::$instance){
                self::$instance = new PDO("mysql:host=".$DBHOST.";dbname=".$DBDB, $DBUSER, $DBPASS);
                self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            return self::$instance;
        }
        private function __clone(){}
    }
    
    
    //assign the database object to the registry, within your initialization
    $registry = new registry;
    $registry->db = db::getInstance(DBHOST,DBDB,DBUSER,DBPASS);
    $registry->router = new router($registry);
    $registry->users = new users($registry);
    
    
    //Then add to your class constructors to have that connection available
    protected $registry;
    function __construct($registry){
        $this->registry=$registry;
    }
    
    //.....
    $result = $this->registry->db->query('SELECT ...')->fetchAll(PDO::FETCH_ASSOC);
    foreach($result as $key=>$val){
        $this->registry->$val['setting']=$val['value'];
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.