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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:06:17+00:00 2026-05-28T19:06:17+00:00

I have a class extending mysqli. It follows a singleton pattern so I have

  • 0

I have a class extending mysqli. It follows a singleton pattern so I have a static method to retrieve the shared instance of the class.

After calling self::$instance = new self I suppose self::$instance should be FALSE or NULL if __construct could not make the connection, but it is not.

The __construct function triggers a WARNING:
Warning: mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket etc.

But self::$instance is created as an instance of BaseDatos class.

How should I detect a failure in the connection and return FALSE on the factory method?

class BaseDatos extends mysqli {

    //singleton, instancia compartida
    private static $instance = null;    
    private $user = "root";
    private $password = "root";
    private $db = "agendaeventos";
    private $dbHost = "localhost";

    public static function getInstance() {
        if (!self::$instance instanceof self) {
            self::$instance = new self;
        }
        if (self::$instance) {
            return self::$instance;
        } else {
            return FALSE; //This is never called even when the connection is not created
        }
    }

    private function __construct() {
        parent::__construct($this->dbHost, $this->user, $this->password, $this->db);
        if (!mysqli_connect_errno()) {
            parent::set_charset('utf8');
        }
    }

}
  • 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-28T19:06:17+00:00Added an answer on May 28, 2026 at 7:06 pm

    A constructor has to create an instance, i.e. it can’t return false or null.

    You can stop the instanciation by throwing an exception in your constructor. For example, if you can’t make the connection to the database, throw an exception and then catch it in getInstance() to return false.

    public static function getInstance() {
        if (self::$instance === null) {
            try {
                self::$instance = new self();
            } catch(Exception $e) {
                self::$instance = false;
            }
        }
        return self::$instance
    }
    private function __construct() {
        parent::__construct($this->dbHost, $this->user, $this->password, $this->db);
        if (!mysqli_connect_errno()) {
            parent::set_charset('utf8');
        } else {
             throw new Exception("Database connection");
        }
    }
    

    However, I recommend you don’t return false in getInstance(), but instead just throw the exception and catch it where the class is used.

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

Sidebar

Related Questions

I have a class extending android.os.Handler . An instance of this handler is passed
I have an intermediary class extending System.Web.UI.Page for all of my pages that require
Currently I have a class that is extending the ListActivity class. I need to
I have two classes in a PHP application, B extending A for example. Class
I have class method that returns a list of employees that I can iterate
Suppose I have class animal and classes cat and dog extending it. I want
I have a problem with extending the mysqli_result class. I am trying to extend
I have an abstract class extending Composite (AbstractWhiteBoard). Then I have a concrete class
I have an abstract class Abstr<T> and an extending class Ext<T> . I need
I have written a class extending UIImageView in order to allow me dynamically generate

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.