【发布时间】:2019-11-27 05:09:44
【问题描述】:
使用 composer 时如何只在另一个文件上需要配置文件?
考虑在这个文件的根目录中有一个名为“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上的变量,写好require“../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....
【问题讨论】:
标签: php composer-php