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');
}
}
}