2017-08-24 31 views
-1

我有兩個文件dbconnect.php和config.php文件如何使用另一個文件中的PHP類

dbconnect.php

<?php 
class connect{ 
    public function __construct(){ 
     $config = require_once __DIR__ . 'config.php'; 
    } 

    private $dbhost = $config['host']; 
    private $dbuser = $config['username']; 
    private $dbpass = $config['pass']; 
    private $dbname = $config['dbname']; 

    public function startConn(){ 
     $this->DBcon = null; 
     try{ 
      $this->DBcon = new PDO("mysql:host=".$this->dbhost.";dbname=".$this->dbname, $this->dbuser, $this->dbpass); 
     }catch(Exception $e){ 
      echo "error connecting:"; 
     } 
     return $this->DBcon; 
    } 
} 

?>

的config.php

<?php 

    /** 
    * Contains all configurations 
    * 
    */ 
    return [ 
    'dbname' => 'user', 
    'pass' => '@user.intern1', 
    'username' => 'user1', 
    'host' => 'localhost', 
    ]; 
?> 

在我的dbconnect.php文件中;
如何從我的config.php中包含變量到類中連接

如果我按照以下方式執行操作:
它大叫我,並給我致命錯誤:
「解析錯誤:語法錯誤,意外的'$ config'(T_VARIABLE)在C:\ xampp \ htdocs \ hngfun \ profile \ adeojoemmanuel \ php-handler \ dbconfig.php第8" 行

+0

最重要的未示出的是是'代碼dbconfig.php'像'私人$ dbhost'不能分配依賴於運行時的數據值,如'$配置[」 – user10089632

+0

聲明的屬性主機'];' –

+0

文件'dbconfig.php'在哪裏? –

回答

1

我正在猜測這裏。但是我可以清楚地看到你在構造函數中將$ config設置爲局部變量。這意味着一旦你離開構造函數,它就不可用。

<?php 
class connect{ 
    public function __construct(){ 
     $config = require_once __DIR__ . 'config.php'; 
     $this->dbhost = $config['host']; 
     $this->dbuser = $config['username']; 
     $this->dbpass = $config['pass']; 
     $this->dbname = $config['dbname']; 
    } 

    private $dbhost ; 
    private $dbuser ; 
    private $dbpass ; 
    private $dbname ; 

    public function startConn(){ 
     $this->DBcon = null; 
     try{ 
      $this->DBcon = new PDO("mysql:host=".$this->dbhost.";dbname=".$this->dbname, $this->dbuser, $this->dbpass); 
     }catch(Exception $e){ 
      echo "error connecting:"; 
     } 
     return $this->DBcon; 
    } 
} 
0

聲明的屬性等private $dbhost不能分配依賴於運行時數據從PHP Docs引述的值,如$config['host'];

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

的溶液,分配VA梅毒在構造函數:

class connect{ 
    public function __construct(){ 
     $config = require_once __DIR__ . 'config.php'; 

     private $this->dbhost = $config['host']; 
     private $this->dbuser = $config['username']; 
     private $this->dbpass = $config['pass']; 
     private $this->dbname = $config['dbname']; 
    } 

    private $dbhost; 
    private $dbuser; 
    private $dbpass; 
    private $dbname; 

    public function startConn(){ 
     $this->DBcon = null; 
     try{ 
      $this->DBcon = new PDO("mysql:host=".$this->dbhost.";dbname=".$this->dbname, $this->dbuser, $this->dbpass); 
     }catch(Exception $e){ 
      echo "error connecting:"; 
     } 
     return $this->DBcon; 
    } 
} 
0

您需要內部構造設置:

private $dbhost; 
private $dbuser; 
private $dbpass; 
private $dbname; 

public function __construct(){ 
    $config = require_once __DIR__ . 'config.php'; 
    $this->dbhost = $config['host']; 
    $this->dbuser = $config['username']; 
    $this->dbpass = $config['pass']; 
    $this->dbname = $config['dbname']; 

} 
0

第一個問題是,你不能從你的config.php文件中返回任何東西。你只能在函數內返回一個結果。實現這一點的一種方法是將數組聲明爲全局變量,然後在需要該配置數組的所有其他php文件中使用它。

<?php 
/** 
* Contains all configurations 
* 
*/ 
$dbconfig = array(
    'dbname' => 'user', 
    'pass' => '@user.intern1', 
    'username' => 'user1', 
    'host' => 'localhost', 
); 
?> 

<?php 
require_once __DIR__ . 'config.php'; 

class connect{ 
    public function __construct(){ 

    } 

    private $dbhost = $dbconfig['host']; 
    private $dbuser = $dbconfig['username']; 
    private $dbpass = $dbconfig['pass']; 
    private $dbname = $dbconfig['dbname']; 

    public function startConn(){ 
     $this->DBcon = null; 
     try{ 
      $this->DBcon = new PDO("mysql:host=".$this->dbhost.";dbname=".$this->dbname, $this->dbuser, $this->dbpass); 
     }catch(Exception $e){ 
      echo "error connecting:"; 
     } 
     return $this->DBcon; 
    } 
} 
?> 
+0

我也這麼認爲。事實證明,您可以從需求中返回全局範圍。不推薦這樣做的技術,但你可以做到這一點。 – ryantxr

+0

include/require確實允許可以分配的返回,如[php docs](http://php.net/manual/en/function.include.php)中所述,並且不限於「全局」範圍....'可以在包含的文件中執行return語句,以便終止該文件中的處理並返回調用它的腳本。另外,可以從包含的文件中返回值。您可以像使用普通函數一樣獲取包含調用的值。 '...實際上,Laravel使用這種方法來配置文件 –

+0

對我來說,學習新東西是一個偉大的夜晚。感謝分享:) –

相關問題