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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:57:46+00:00 2026-05-28T00:57:46+00:00

So currently my class DATABASE uses a singleton for a connection and is called

  • 0

So currently my class DATABASE uses a singleton for a connection and is called in the constructor of my class SQL. I have 4 other classes that extend class SQL for access to its methods. The problem is, the children instantiate each other in some cases so I’m afraid I’m creating multiple class SQL objects because they call their parent constructor by default, aka class SQL. I read about dependency injection, but no clear how to implement it in my situation so this is what I was thinking.
Have all classes extend class DATABASE and make class SQL methods static.
But then I would have to call the singleton connection in class DATABASE, on its own class as so.

class DATABASE {

    function __construct() {
        self::getInstance();
        /// or DATABASE::getInstance ( im not quite positive which way )
    }

    public static function getInstance() { 
        if (self::$instance == false) { 
        self::$instance = new DATABASEMANAGER(); 
        } 

        return self::$instance; 
    } 
}

I figure this way, since all classes extend class DATABASE, and instantiate each other, I know I am not creating multiple objects.
I hope this makes sense and I can elaborate more if you need me to, but does this make since as an approach to my problem?

  • 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-28T00:57:47+00:00Added an answer on May 28, 2026 at 12:57 am

    I guess you building your class other way around, this approach is more consistent in my opinion.

    class DATABASE extends DATABASEMANAGER
    {
        static $instance;
    
        private function __construct()
        {
            parent::__construct();
        }
    
        public static function getInstance()
        {
            if (empty(self::$instance))
            {
                self::$instance = new self();
            }
    
            return self::$instance;
        }
    }
    

    Usage:

    class Blog
    {
        public function __construct()
        {
            $this->database = DATABASE::getInstance();
        }
    
        public function getRecord($id)
        {
    
            $result = $this->database->query("SELECT * FROM blog WHERE id='{$id}'");
            //or
            $result = DATABASE::getInstance()->query("SELECT * FROM blog WHERE id='{$id}'");
    
            //other actions...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I have a database access class named DB , which uses PDO .
I currently have a legacy system that uses SPs exclusively for access to the
I have an app that uses an sqlite database. Currently I have a database
The Scenario Currently I have a C# Silverlight Application That uses the domainservice class
I'm currently writing a class that implements the SeekableIterator interface and have run into
I have a class that currently has several methods that take integer parameters. These
Working with Azure web role that communicates to a SQL Azure database. Currently when
I have a base class that declares a private non-static reference to the DataBase
I have a database that uses natural keys (i.e. business oriented keys). Unfortunately, because
I currently have a custom log manager that internally uses java.util.Formatter . The main

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.