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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:12:15+00:00 2026-05-12T19:12:15+00:00

I am going to use singleton classes to manage both DB connections and references

  • 0

I am going to use singleton classes to manage both DB connections and references to application settings.

It seems a little messy to have to use the following code in every method in order to access the db class.

$db = DB::getInstance();

Is there a more efficient way of going about it?

Any advice appreciated.

Thanks

  • 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-12T19:12:15+00:00Added an answer on May 12, 2026 at 7:12 pm

    I often use the Registry pattern, where this behavior occurs as well. I always set a instance variable in the constructor of my models to point to the Registry entry;

    class Registry {
        private static $_instance;
        private $_registry;
    
        private function __construct() {
            $_registry = array();
        }
    
        public static function getInstance() {
            if (!Registry::$_instance) {
                Registry::$_instance = new Registry();
            }
    
            return Registry::$_instance;
        }
    
        public function add($key, &$entry) {
            $this->_registry[$key] = &$entry;
        }
    
        public function &get($key) {
            return $this->_registry[$key];
        }
    
        public function has($key) {
            return ($this->get($key) !== null);
        }
    }
    

    Model example;

    class MyModel {
        private $_db;
        public function __construct() {
            $this->_db = Registry::getInstance()->get('dbKey');
        }
    
        /* Every function has now access to the DAL */
    }
    

    Instantiation example;

    $dal = new Db(...);
    Registry::getInstance()->add('dbKey', $dal);
    ...
    $model = new MyModel();
    $model->doDbStuff();
    

    Another approach is to always pass the reference as a parameter to each constructor.

    Of course I only use this behavior when most of the methods in my model use the reference, if only a few (one or two) methods have use of the reference, I call the Registry/Singleton like you showed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going to use heavily some C-API in my C++ application, I have
we are going to use mongo db for an alert monitoring application. We thought
I have a web service deployed and ready to use. The client application is
I was going to use the following line of code: [[NSSortDescriptor alloc] initWithKey:@title ascending:YES
I am going to use Vaadin UI framework to develop web application where indexing
We are going to use Git where I work, and we have some in
im going to use Sociallib library in my app to interact with social networks.
I was going to use Zend_Cache, but it says here that it's not the
I am going to use a HSlider to set a range of values. I
I was just going to use the FileUpload.FileBytes property but looking into the respective

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.