Tiny Config Function in Dot Notation

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

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");

Previous
MySQL. Script to Convert Old Dates to New
Next
Interesting "Rules of Management"

Keep Reading

Explore more articles and insights

Back to Golang: Why I Switched From Static to Dynamic Again

Back to Golang: Why I Switched From Static to Dynamic Again

Read Article
Associations and Eager Loading in Go — Without GORM

Associations and Eager Loading in Go — Without GORM

Read Article
Soft Deletes Done Right — Three Strategies for Go

Soft Deletes Done Right — Three Strategies for Go

Read Article
View All Blog Posts