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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:52:29+00:00 2026-05-25T10:52:29+00:00

All the singleton patterns I have seen use a reference to the object to

  • 0

All the singleton patterns I have seen use a reference to the object to determine if the object has been instantiated. However, if I am using a singleton to guarantee only one db connection, why not use the db connection resource link to do this? Here is the code I am using. (PS: it works fine). I use the comment to be able to search for my classes easily.

/*one*/

class one
  {
  public static $db;
  private function __construct()
    {
    self::$db=new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
    }
  public static function get()
    {
    if(self::$db==NULL)
      {
      new self();
      }
    return self::$db;
    }
  }
  • 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-25T10:52:30+00:00Added an answer on May 25, 2026 at 10:52 am

    In PHP, a constructor does not return.

    So your get method returns a one object, the first time it’s called, then a mysqli object. Probably not what you want.

    if( self::$_db == NULL )
    {
        return new self(); // Here you return an object of class one
    }
    else
    {
        return self::$_db; // Here you return an object of type mysqli
    }
    

    If you want to return the mysqli object, you do not need a singleton, as there is no need to create an instance of an object that’s only here to return an instance of another object.

    The registry pattern would be better in such a case.

    If you need to provide methods (a wrapper for your DB object), then create a real singleton.

    EDIT

    I checked your updated code.
    Now you return always the mysqli instance. But you do not need to instantiate your own object. That’s completely useless…

    If you really want to go with your kind of pattern, as golden said, in your static instance, checks if self::db is NULL. If yes, creates the mysqli instance, and assign it to self::db. Then returns it.

    public static getDatabaseInstance()
    {
        if( self::$_db == NULL )
        {
            self::$_db = new mysqli( ... );
        }
    
        return self::$_db;
    }
    

    Also set the constructor private, so users won’t be able to create useless instances of your class. Or better make it public and throw an exception:

    public function __construct()
    {
        throw new Exception( 'This class is not supposed to be instantiated' );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the singleton pattern in all my PHP class files. Would it
In all my projects till now, I use to use singleton pattern to access
i have noticed that every area has some tools you can use to make
I have asked a few question lately about the use of the singleton and
I have a singleton object called PoolManager that loads and saves some data in
I have many forms on an solution in .Net compact framework using all the
I have a number of classes doing different things but using the same Singleton
Most of my programming has been in web-based applications, although I have done some
The Singleton and the Registry patterns were very simple and easy for me to
I have a fairly large CRUD WinForm app that has numerous objects. Person, Enrollment,

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.