【问题标题】:How to import a config.php file in a php script [duplicate]如何在 php 脚本中导入 config.php 文件 [重复]
【发布时间】:2019-08-30 07:45:39
【问题描述】:

我尝试将 config.php 读入 php 类,但做不到。

我只想在真正需要时才加载配置,而不是在程序开始时导入它

我在网上搜索了很多,但没有找到任何解释。

我会向你展示我的班级,我在那里阅读了配置:

<?php declare(strict_types=1);

namespace Mailer\Mailer;

class Mailer
{
    private $mailConfigs;

public function __construct()
{
    $this->mailConfigs = require __DIR__ . '/../../Config/MailConfiguration.php';
}

/**
 * @return mixed
 */
public function getMailConfig()
{
    return $this->mailConfigs;
}
}

我的索引文件如下所示:

<?php
use Maier/Mailer;

$mailer = new Mailer();
$mailer->getMailConfig();

var_dump($mailConfiguration);

不幸的是,这段代码只给了我找不到配置的错误。

然后我以为config dn只能在Mailer.php类中使用,但即使那样,我也只会收到他找不到配置的错误消息:

<?php declare(strict_types=1);

namespace Mailer\Mailer;

class Mailer
{
    private $mailConfigs;

public function __construct()
{
    $this->mailConfigs = require __DIR__ . '/../../Config/MailConfiguration.php';
}

/**
 * @return mixed
 */
public function getMailConfig()
{
    $this->mailConfigs;
    var_dump($mailConfig);
}
}

顺便说一句。我的配置如下所示:

<?php

$mailConfig = [
    'mailSender' => 'wk@someSender.com',
    'mailSubject' => 'XXX',
    'maxConnections' => 400,
    'maxTime' => 600,
    'csv' => 'mailaccounts.csv',
];

有人可以帮我解释一下如何在不全局读取的情况下正确访问 oop 中配置中的数组吗?

非常感谢。

【问题讨论】:

    标签: php require


    【解决方案1】:

    代码的问题在于您实际上并没有在MailConfiguration.php 中返回配置。你将它定义为一个变量,你永远不会在构造函数中使用它。

    不要将配置数组存储在配置文件中的变量中,而是:

    return [
        'mailSender' => 'wk@someSender.com',
        'mailSubject' => 'XXX',
        'maxConnections' => 400,
        'maxTime' => 600,
        'csv' => 'mailaccounts.csv',
    ];
    

    现在配置数组将被加载到您的构造函数中的$this-&gt;mailConfigs

    然后,当您想使用它时,只需:

    public function getMailConfig()
    {
        return $this->mailConfigs;
    }
    

    因为它包含与您在配置文件中返回的相同的数组。

    建议

    最好构建配置,这样您就可以为不同的环境(产品、暂存、本地、测试等)加载不同的配置文件。因此建议您将配置文件的路径传递给构造函数。然后你可以为不同的环境传递不同的文件。示例:

    public function __construct($configFile)
    {
        $this->mailConfigs = require $configFile;
    }
    

    当你实例化类时:

    $mailer = new Mailer(__DIR__ . '/../../Config/MailConfiguration.php');
    

    然后你可以让你传入的文件以当前环境为条件。

    【讨论】:

      【解决方案2】:

      另一种方法是以不同的格式保存配置,即对其进行序列化并在需要时对其进行反序列化。一种使用的格式是 JSON 格式。

      例子

      index.php

      <?php
          include 'loader.php';
      
          $loader = new loader();
      ?>
      

      加载器.php

      <?php
          class loader{
              private $config;
      
              function __construct(){
                  $this->config = file_get_contents('config.php');
                  $this->config = json_decode($this->config,true);
      
                  echo $this->config['a']; //Test Config
                  echo $this->config['b']; //Test B
              }
          }
      ?>
      

      Config.php (JSON)

      {
          "a":"Test Config",
          "b":"Test B"
      }
      

      输出: 测试配置测试B

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-21
        • 1970-01-01
        • 2012-07-04
        • 1970-01-01
        • 1970-01-01
        • 2012-12-01
        • 1970-01-01
        • 2016-10-07
        相关资源
        最近更新 更多