dongxili9934 2012-05-01 16:56
浏览 51
已采纳

OpenCart配置类如何工作?

Iam was browsing the code for OpenCart. I found a library class file called. config.class.php.

here is the code:

public function load($filename)
{
    $file = SYS_CONFIG_DIR . $filename . '.php';
    if(file_exists($file))
    { 
        $cfg = array();
        require($file);
        $this->data = array_merge($this->data, $cfg);
    }
    else
    {
        trigger_error('Error: Could not load config ' . $filename . '!');
        exit();
    }
}

I can see it first tries to check if the file exist. then a creates a var ($cfg) as an array. then it requires the file. then it merges its. This is where i dont understand.

$this->data = array_merge($this->data, $cfg);

so my config file that i am loading into this class. how would i stucture it so it will be able to merge it with this system config class?

  • 写回答

2条回答 默认 最新

  • dragon202076 2012-05-01 17:14
    关注

    Take a look at the PHP documentation of array_merge, it says exactly, how it works:

    If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

    This basically means that in your config (loaded later) you can have an empty $cfg array, then it will do nothing. If you set some variable in the config array:

    $cfg = array();
    $cfg["var"] = "value";
    

    it will either create a new setting (if it was not set before) or it will overwrite such setting.

    Of course if you load another config after it will again overwrite whatever values are set in both the configs. Last one wins.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证