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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:49:09+00:00 2026-05-26T13:49:09+00:00

class SingleTon { private static $instance; private function __construct() { } public function getInstance()

  • 0
class SingleTon
{
    private static $instance;

    private function __construct()
    {
    }

    public function getInstance() {
        if($instance === null) {
            $instance = new SingleTon();
        }
        return $instance;
    }
}

The above code depicts Singleton pattern from this article. http://www.hiteshagrawal.com/php/singleton-class-in-php-5

I did not understand one thing. I load this class in my project, but how would I ever create an object of Singleton initially. Will I call like this Singelton :: getInstance()

Can anyone show me an Singleton class where database connection is established?

  • 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-26T13:49:09+00:00Added an answer on May 26, 2026 at 1:49 pm

    An example of how you would implement a Singleton pattern for a database class can be seen below:

    class Database implements Singleton {
        private static $instance;
        private $pdo;
    
        private function __construct() {
            $this->pdo = new PDO(
                "mysql:host=localhost;dbname=database",
                "user",
                "password"
            );
        }
    
        public static function getInstance() {
            if(self::$instance === null) {
                self::$instance = new Database();
            }
            return self::$instance->pdo;
        }
    }
    

    You would make use of the class in the following manner:

    $db = Database::getInstance();
    // $db is now an instance of PDO
    $db->prepare("SELECT ...");
    
    // ...
    
    $db = Database::getInstance();
    // $db is the same instance as before
    

    And for reference, the Singleton interface would look like:

    interface Singleton {
        public static function getInstance();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code running on PHP 5.2.6 class Singleton { static private $instance
So I have the following: public class Singleton { private Singleton(){} public static readonly
I have the following class class Singleton { private: static Singleton *p_inst; Singleton(); public:
I saw the following code: http://sourcemaking.com/design_patterns/singleton/cpp/1 class GlobalClass { private: int m_value; static GlobalClass
If a singleton is implemented as follows, class Singleton { private static Singleton instance
I write a singleton c++ in the follow way: class A { private: static
The default way to implement singleton pattern is: class MyClass { private static MyClass
In other words, is this Singleton implementation thread safe: public class Singleton { private
So, I've always implemented a singleton like so: class Singleton { private static $_instance
I have an AS3 Singleton: package { public class Singleton { public function Singleton(enforcer:SingletonEnforcer):void

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.