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

  • Home
  • SEARCH
  • 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 7784683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:08:02+00:00 2026-06-01T20:08:02+00:00

I am confused on how a singleton model vs a static model works for

  • 0

I am confused on how a singleton model vs a static model works for database connections. My friend created a “static” class and showed me it but it did not make any sense on how it was static. I kind of understand the singleton method of how to create a database connection, but i’m not sure it meets my goal.

The main thing I want to do is cut down on the number of connections opened to MYSQL. I have a class with a function that calls the database quiet frequently, and there is no reason for it to make a new connection each time someone requests something that requires the database. Could someone provide a small example class for doing this with the singleton or the static method (whichever is the correct approach) that connects to the database and shows a small sample query? I would greatly appreciate it.

Oh yeah, I am using PHP 5.3 🙂 Please feel free to ask for additional details.

  • 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-01T20:08:04+00:00Added an answer on June 1, 2026 at 8:08 pm

    Consider the following example which uses the singleton design pattern to access the instance of database object.(purpose of this is to reuse the same connection again and again through out the application)

    class Database {
    
        protected static $_dbh;
        const HOST = 'localhost';
        const DATABASE = 'dbname';
        const USERNAME = 'username';
        const PASSWORD = 'password';
    
        //declare the constructor as private to avoid direct instantiation.   
        private function __construct() { }
    
        //access the database object through the getInstance method.
        public static function getInstance() {
            if(!isset($_dbh)) {
                #Connection String.
                self::$_dbh = new PDO('mysql:host='.self::HOST.';dbname='.self::DATABASE,self::USERNAME,self::PASSWORD);
                self::$_dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            return self::$_dbh;
        }
    }
    

    now if i have to make use of the class anywhere in the application i would simple do it like this.

    require_once('database.php');
    $dbh = Database::getInstance();
    $sth = $dbh->query('SELECT * FROM sometable');
    $result = $sth->fetchAll(PDO::FETCH_ASSOC);
    

    the call to Database::getInstance(); uses static method. what this basically does is it restricts you from directly instantiating the object by declaring the constructor as private, and instead it checks if the object is already instanitated. if true then return already instantiated object. else create new and return newly created object. this makes sure that same database connection is reused through out the application.

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

Sidebar

Related Questions

I am confused: In AS3, why do we keep the Singleton class constructor public
It was already asked what's the difference between singleton and static class. However, knowing
Bit confused here, I have an on-demand instance but do I get charged even
I confused with extends, i went with theory n solved some examples, But when
Ok guys I am developing an iPhone app I have a Model class which
namespace AV.Connections { protected class MyConnection { protected ConnectionStringSettings connectionSettings { get { return
Very confused. In my workspace , the trunk works fine when I do a:
Okay i have seen TouchXML, parseXML, NSXMLDocument, NSXMLParser but i am really confused with
Here is my singleton code: @synthesize listOfSites; + (id)sharedInstance { static dispatch_once_t dispatchOncePredicate =
Confused regarding best methodology to make website usable and consistent across all devices -

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.