簡體   English   中英

在類php中包含文件

[英]Including file inside a class php

我第一次使用php中的類做某事。 我試圖獲取類中的return object array項。

這是我的課

class User {

    $dbconn = include("config.php");
    private $dbHost     = $dbconn->host;
    private $dbUsername = $dbconn->username;
    private $dbPassword = $dbconn->pass;
    private $dbName     = $dbconn->database;
    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;
            }
        }
    }
}

這是我的config.php文件

return (object) array(
    'host' => 'localhost',
    'username' => 'my_user',
    'pass' => 'my_pass',
    'database' => 'my_db'
);

我該怎么做?

PHP Parse error:  syntax error, unexpected '$dbconn' (T_VARIABLE)

您不能在變量定義中包含可執行代碼,而只能是靜態值。 因此不支持這種事情:

class foo {
    public $var = result_of_some_function();
}

如果要初始化值,請使用構造函數。 您最好將其作為配置文件讀取:

class User {
    public function __construct() {
        $config = json_decode(file_get_contents('config.json'));
        $conn = new mysqli($config->host, ...);
    }
}

或者更好,使用依賴注入:

class User {
    protected $db = null;
    public function __construct($db) {
        $this->db = $db;
    }
}

然后在您的代碼中創建一個用戶對象:

$db = new Db($config);
$user = new User($db);

另一種方法是在配置文件中定義常量,並在類中使用它們。

在config.php文件中

define('HOST', 'localhost');
define('USERNAME', 'my_user');
define('PASS', 'my_pass');
define('DATABASE', 'my_db');

在班級檔案中

include("config.php")
class User {
    private $dbHost     = HOST;
    private $dbUsername = USERNAME;
    private $dbPassword = PASS;
    private $dbName     = DATABASE;
    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;
            }
        }
    }
}

包括以下內容:

 $dbconn = include("config.php");

在您的構造函數中。

也許您應該將代碼更改為

function __construct()
    {
        //included db file
        include 'config.php'; 
        if (!isset($this->db))
        { 
//code here
}

嘗試使用這種方式。

<?php
class User
{

    private $dbconn = null;
    private $dbHost;
    private $dbUsername;
    private $dbPassword;
    private $dbName;
    private $userTbl = 'users';

    function __construct()
    {
        include 'config.php'; //included file in constructor
        if (!isset($this->db))
        {
            $this->dbHost= $this->dbconn->host;
            $this->dbUsername= $this->dbconn->username;
            $this->dbPassword= $this->dbconn->pass;
            $this->dbName= $this->dbconn->database;
            // 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;
            }
        }
    }

}

Config.php

<?php
$this->dbconn= (object) array(
    'host' => 'localhost',
    'username' => 'my_user',
    'pass' => 'my_pass',
    'database' => 'my_db'
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM