dongshenghe1833 2019-07-18 09:42
浏览 60

如何在使用composer时仅在特定文件上制作“require”-config文件

How require config file on only on another file when use composer?

Consider have a file called "config.php" in root in this file we have some variable like $site_url :

$config = [
    // e.g (http://example.com or https://exmaple.com)
    "site_url" => "http://localhost"
];

and have a class called "DBConnect" in include folder "include/DBConnect.php" now we want use variable saved on config.php in class "DBConnect" , well write require "../config.php"; and use variable.

<?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();

now we call getname class in index.php , when called it php return error

Warning: require(../config.php): failed to open stream....
  • 写回答

1条回答 默认 最新

  • dongquan8753 2019-07-18 09:52
    关注

    Make path relative to current directory by using __DIR__ magic constant:

    require __DIR__ . "/../config.php";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数