Blog

Tiny Config Function in Dot Notation

This is a tiny PHP function to read an array configiration file, and return the key in a dot notation

function config($key, $default = null) {
    static $config = null;
    // Is already read? No=>Read config
    if ($config == null) {
       $config = include __DIR__ . '/config.php';
    }
    $temp = $config;
    $path = explode(".", $key);
    foreach ($path as $key) {
       if (isset($temp[$key])) {
           $temp = $temp[$key];
           continue;
       }
        return $default;
     }
     return $temp;
}

Example:

config("app.name","Default name");