我创建了一个数据库连接脚本,如下所示:
class DBManager{
static $connection;
public $dbh;
private $dbHost = null, $dbUser = null, $dbPass = null, $dbName= null;
private function __construct($dbDetails = array()){
$this->dbHost = $dbDetails['db_host'];
$this->dbName = $dbDetails['db_name'];
$this->dbUser = $dbDetails['db_user'];
$this->dbPass = $dbDetails['db_pass'];
$this->dbh = new PDO("mysql:host=$this->dbHost;dbname = $this->dbName", $this->dbUser, $this->dbPass);
}
public static function init($dbDetails){
if(isset(self::$connection)){
return self::$connection;
}
else{
return self::$connection = new DBManager($dbDetails);
}
}
}
$dbDetails = array(
'db_host' => '127.0.0.1',
'db_name' => 'loura',
'db_user' => 'root',
'db_pass' => ''
);
我希望通过以下方式在我的其他课程中获得连接:
require_once('test.php');
class Data{
private $dbh;
public function __construct($dbDetails){
$this->dbh = DBManager::init($dbDetails);
}
function getInfo(){
$stmt = $this->dbh->prepare("SELECT * FROM user_master");
$stmt->execute();
return $stmt -> fetchAll();
}
}
我这样做是否正确?我试图在另一个类中调用静态方法,这是合法的吗?
编辑:这是我的错误:
致命错误:未捕获错误:调用未定义的方法DBManager :: prepare()
答案 0 :(得分:0)
DBManager类中存在类型多态问题:
{{1}}
答案 1 :(得分:0)
我建议这样的事情。
// DBConfig.php
namespace App\Model\Db;
class DBConfig {
public static $dbConfig = [
'db_host' => '127.0.0.1',
'db_name' => 'loura',
'db_user' => 'root',
'db_pass' => '',
];
}
// DBConnection.php
namespace App\Model\Db;
class DBConnection {
private $dbh = null;
private $dbHost = null, $dbUser = null, $dbPass = null, $dbName= null;
public function getDbh()
{
return $this->dbh;
}
/**
* Setting up connection on class create.
*/
public function __construct($dbDetails = array()){
$this->dbHost = $dbDetails['db_host'];
$this->dbName = $dbDetails['db_name'];
$this->dbUser = $dbDetails['db_user'];
$this->dbPass = $dbDetails['db_pass'];
$this->dbh = new PDO("mysql:host=$this->dbHost;dbname = $this->dbName", $this->dbUser, $this->dbPass);
}
}
// DBManager.php
namespace App\Model\Db;
class DBManager {
private $connection = null;
public static getConnection()
{
if ($this->connection instanceof DBConnection) {
return $this->connection;
} else {
$this-connection = new DBConnection(DBConfig::dbConfig);
return $this->connection;
}
}
}
// Data.php
namespace App\Model\Data;
use App\Model\DBManager;
class Data {
function getInfo(){
$conn = DBManager::getConnection();
$stmt = $conn->prepare("SELECT * FROM user_master");
$stmt->execute();
return $stmt -> fetchAll();
}
}
答案 2 :(得分:0)
return self::$connection = new DBManager($dbDetails);
上有错误,此处返回的是DBManager
个实例,而不是PDO
个实例。
class DBManager{
static $connection;
public $dbh;
private $dbHost = null, $dbUser = null, $dbPass = null, $dbName= null;
private function __construct($dbDetails = array()){
$this->dbHost = $dbDetails['db_host'];
$this->dbName = $dbDetails['db_name'];
$this->dbUser = $dbDetails['db_user'];
$this->dbPass = $dbDetails['db_pass'];
$this->dbh = new PDO("mysql:host=$this->dbHost;dbname = $this->dbName", $this->dbUser, $this->dbPass);
}
public static function init($dbDetails){
if(isset(self::$connection)){
return self::$connection;
}
else{
self::$connection = new self($dbDetails);
return self::$connection->dbh;
}
}
}
但我建议使用以下class
/**
* PDO database, only one connection is allowed.
*
*/
class Db {
//
private $_dbh;
// Store the single instance.
private static $_instance;
/**
* Get an instance of the database.
* @return Db.
*/
public static function instance(){
if( !self::$_instance ){
self::$_instance = new self();
}
return self::$_instance->_dbh;
}
/**
* Magic __construct.
*/
function __construct(){
try{
$this->_dbh = new PDO( "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET, DB_USER, DB_PASSWORD );
}catch( PDOException $e ){
trigger_error( $e->getMessage().PHP_EOL, E_USER_ERROR );
die( 'Error: Can\'t connect to the database.' );
}
}
/**
* Empty clone method to prevent duplication.
*/
function __clone(){}
}