简体   繁体   中英

OpenCart How does the config class work?

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?

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.

You basically need to create a php file and define the $cfg array with key value pairs. Example

<?php

$cfg['some_var'] = 'value';
$cfg['som_other_var'] = 'some other value';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM