在 PHP class 中包含 MySQL 配置文件

include MySQL config file inside PHP class

我想在 PHP class.

中包含 MySQL 配置文件
<?php
class User {
    private $dbHost     = "localhost";
    private $dbUsername = "root";
    private $dbPassword = "";
    private $dbName     = "gic";
    private $userTbl    = 'users';

    function __construct(){
        if(!isset($this->db)){
            // Connect to the database
            $conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);
            if($conn->connect_error){
                die("Failed to connect with MySQL: " . $conn->connect_error);
            }else{
                $this->db = $conn;
            }
        }
    }

    function checkUser($userData = array()){
        if(!empty($userData)){

            $host  = $_SERVER['HTTP_HOST'];
            $host_upper = strtoupper($host);
            $path   = rtrim(dirname($_SERVER['PHP_SELF']), '/\');
            $baseurl = "http://" . $host . $path ."/";


            $prevQuery = "SELECT * FROM ".$this->userTbl." WHERE oauth_provider = '".$userData['oauth_provider']."' AND oauth_uid = '".$userData['oauth_uid']."'";
            $prevResult = $this->db->query($prevQuery);

           <!-- rest of my code --> 
        }
    }
}

当我 运行 这个文件时,它工作正常,但我想将 MySQL 数据库连接分离到另一个文件 (config.php),所以我编辑了我的代码:

<?php
class User {

include 'config.php';

$connection = new createCon();
$conn = $connection->connect();

function checkUser($userData = array()){
    if(!empty($userData)){

        $host  = $_SERVER['HTTP_HOST'];
        $host_upper = strtoupper($host);
        $path   = rtrim(dirname($_SERVER['PHP_SELF']), '/\');
        $baseurl = "http://" . $host . $path ."/";


        $prevQuery = "SELECT * FROM ".$this->userTbl." WHERE oauth_provider = '".$userData['oauth_provider']."' AND oauth_uid = '".$userData['oauth_uid']."'";
        $prevResult = $conn->query($prevQuery);
        }
    }
}

和config.php

<?php

class createCon  {
    var $host = 'localhost';
    var $user = 'root';
    var $pass = '';
    var $db = 'gic';
    var $myconn;

    function connect() {
        $con = mysqli_connect($this->host, $this->user, $this->pass, $this->db);
        if (!$con) {
            die('Could not connect to database!');
        } else {
            $this->myconn = $con;
            echo 'Connection established!';}
        return $this->myconn;
    }

    function close() {
        mysqli_close($myconn);
        echo 'Connection closed!';
    }
}
?>

当我运行上面的代码时,它显示错误:

Parse error: syntax error, unexpected 'include' (T_INCLUDE), expecting function (T_FUNCTION) or const (T_CONST) in C:\xampp\htdocs\gic001\User.php on line 4

有什么想法吗?

尝试将包含移到 class 之外。

<?php

require('config.php');

class User {

否则将它放在一个函数中:

class User {

 protected $conn;       


  public function __construct(){
    $this->loadConfig();
  }


  protected function loadConfig(){
     require('config.php');
     $connection = new createCon();
     $this->conn = $connection->connect();
  }

您还需要了解以下内容:

有了继承,应该问题不大。保持你的配置文件不变,试试这个。

include_once 'config.php';

class User extends createCon {

    private $conn;

    function __construct(){
        $this->conn = $this->connect();
    }
}

坦率地说,你的整个方法都是错误的。

A user is not a database! 用户 class 中不应该有这样一个 属性 作为 $dbHost。它也不应该费心连接到数据库。用户 class 应该只使用别处定义的连接。

您应该创建一个数据库实例,一次。然后将它作为参数传递给用户的构造方法

class User {
    protected $conn;
    public function __construct($conn) {
        $this->conn = $conn;
    }
    function checkUser($userData = array()){
        $prevResult = $this->conn->query($prevQuery);
    }
}

因此,只需在创建连接的位置添加您的凭据一次。