我想使用单一设计模式使用已建立的连接。 我需要有关如何实现这一点的帮助,因为我无法查询某些数据
db.config.php:
define ('HOST','localhost');
define ('USER','garodamas_mon');
define ('PASSWORD','r0d4m45');
define ('DATABASE','garodamas_cashrcv');
class Database{
private $DBH;
private static $singleton;
protected function __construct(){
$this->DBH=new mysqli(HOST,USER,PASSWORD,DATABASE);
}
public static function instance(){
if (!(self::$singleton instanceof self)) {
self::$singleton = new self();
}
return self::$singleton;
}
public static function get(){
return self::instance()->DBH;
}
private function __wake(){}
private function __clone(){}
}
和loginmodel.php:
require_once '../db.config.php';
$db = DATABASE::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT user_name FROM mst_user";
$result = $mysqli->query($sql_query);
,错误信息是,
致命错误:在第5行的C:\ xampp \ htdocs \ SOBCASHIER \ resources \ templates \ loginmodel.php中调用未定义的方法Database :: getInstance()
答案 0 :(得分:3)
您没有getInstance
方法 - 您定义的方法称为instance
:
$db = DATABASE::instance();