簡體   English   中英

如何在php類中使用另一個文件

[英]how to use another file inside a php class

我有兩個文件dbconnect.php和config.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中的變量包含到類connect中

如果我按照上面的方式做;
它對我大吼,並給我致命錯誤:
“解析錯誤:語法錯誤,在第8行的C:\\ xampp \\ htdocs \\ hngfun \\ profile \\ adeojoemmanuel \\ php-handler \\ dbconfig.php中出現意外的'$ config'(T_VARIABLE)”

我在這里猜。 但是我可以清楚地看到,您正在將$ 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;
    }
} 

不能為依賴於運行時數據的已聲明屬性(如private $dbhost分配值,例如$config['host']; private $dbhost

引用PHP文檔

該聲明可以包括一個初始化,但是此初始化必須是一個常量值-也就是說,它必須能夠在編譯時進行評估,並且必須不依賴於運行時信息才能進行評估。

解決方案,在構造函數中分配值:

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;
    }
} 

您需要在構造函數內部進行設置:

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'];

}

第一個問題是您不能只從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;
    }
} 
?>

暫無
暫無

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

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