如何在php类中使用另一个文件

时间:2017-08-24 21:54:05

标签: php sql database

我有两个文件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;
    }
} 

&GT;

的config.php

<?php

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

在我的dbconnect.php文件中;
如何将config.php中的变量包含到连接类

如果我按以下方式这样做;
它对我大吼大叫,给了我致命的错误:
    &#34;解析错误:语法错误,意外&#39; $ config&#39; (T_VARIABLE)在第8行和第34行的C:\ xampp \ htdocs \ hngfun \ profile \ adeojoemmanuel \ php-handler \ dbconfig.php中;

4 个答案:

答案 0 :(得分: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;
    }
} 

答案 1 :(得分:0)

不能为private $dbhost这样的已声明属性分配依赖于运行时数据的值,例如$config['host'];

引用PHP Docs

  

此声明可能包括初始化,但此初始化必须是常量值 - 也就是说,它必须能够在编译时进行评估,并且必须不依赖于运行时信息才能进行评估。

解决方案,在构造函数中指定值:

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

答案 2 :(得分: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'];

}

答案 3 :(得分: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;
    }
} 
?>