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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:27:14+00:00 2026-05-22T23:27:14+00:00

This is the singleton pattern also named as the singleton class. Its goal is

  • 0

This is the singleton pattern also named as the singleton class. Its goal is to allow only one object of type singleton. If there is already one and I call it, I’d like it to error out in some way. This won’t happen in production but in development. Is there a better way then just saying echo echo “Error:only one connection”;

class singleton
  {
  protected static $_db_pointer = NULL;
  private function __construct()
    {
    $this->_db_pointer = mysql_connect(DB_HOST, DB_USER, DB_PASS);
    mysql_select_db(DB_DATABASE);
    }
  public static function get_instance()
    {
    if(self::$_db_pointer == NULL)
      {
      return new self();
      } 
    else
      {
      echo "Error:only one connection";
      }
    }
  }
  • 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-22T23:27:15+00:00Added an answer on May 22, 2026 at 11:27 pm

    Exceptions usually are better.

    else
    {
      throw new Exception("Error:only one connection");
    }
    

    You can also use “LogicException”, “RuntimeException”, and a few others.

    Further reading:
    http://www.php.net/manual/en/language.exceptions.php

    Another approach with singleton class is just to return the object instead of creating a new instance if one exists.

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

Sidebar

Related Questions

I implemented a Singleton pattern like this: public sealed class MyClass { ... public
There are already quite some posts about the Singleton-Pattern around, but I would like
In other words, is this Singleton implementation thread safe: public class Singleton { private
I have a singleton class for global access to config information. This singleton class
This question is about using getter methods of a singleton object in worker threads.
I was reading up on singleton class design in C# on this great resource
i used to create an instance of a singleton class like this: $Singleton =
I've created a singleton class that loads a plist. I keep getting this error
What are the scenarios when one would use a Monostate pattern instead of singleton
Possible Duplicate: Difference between static class and singleton pattern? we use static class for

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.