<?
class DbConn {
const DB_HOST = 'localhost';
const DB_USER = 'root';
const DB_PASS = 'pw';
const DB_NAME = 'db';
static private $instance = NULL;
private $_db;
static function getInstance()
{
if (self::$instance == NULL)
{
self::$instance = new DbConn();
if(mysqli_connect_errno()) {
throw new Exception("Database connection failed: ".mysqli_connect_error());
}
}
return self::$instance;
}
private function __construct()
{
$this->_db = new mysqli(self::DB_HOST, self::DB_USER, self::DB_PASS, self::DB_NAME) or die('Couldnt connect');
}
private function __clone() {}
}
DbConn::getInstance()->query();
?>
Im just learning and know I’ve got to singleton pattern, I tried to make my own DBConnect class using singleton, but when I’m trying executing it, it gives an error when calling DbConn::getInstance()->query(); saying that Call to undefined method DbConn::query() What am I doing wrong?
Try adding the following after the
__constructfunctionthen try