使用作曲家时,如何仅在另一个文件上要求配置文件?
考虑在该文件的根目录中有一个名为“ config.php”的文件,我们有一些变量,例如$ site_url:
$config = [
// e.g (http://example.com or https://exmaple.com)
"site_url" => "http://localhost"
];
,并且在包含文件夹“ include / DBConnect.php”中有一个名为“ DBConnect”的类,现在我们要使用保存在类“ DBConnect”中的config.php上的变量,编写要求为“ ../config.php”;并使用变量。
<?php
namespace Admin\includes;
require "../config.php";
class DBConnect
{
//dsn : engine:Servername;DbName;charset;
protected $engine;
protected $servername;
protected $dbname;
protected $charset;
public function __construct()
{
}
}
index.php
<?php
require "vendor/autoload.php";
$tp1 = new \Admin\includes\DBConnect();
现在我们在index.php中调用getname类,当调用它时php返回错误
Warning: require(../config.php): failed to open stream....