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条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条