2017-08-24 62 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

对我来说,学习新东西是一个伟大的夜晚。感谢分享:) –

相关问题