From bd7b541c31366902b5b09f1924811f2db6ba5d1d Mon Sep 17 00:00:00 2001 From: Keyur Ajmera Date: Sat, 17 Oct 2015 21:01:47 -0700 Subject: [PATCH] Add global function 'getConfig' --- README.md | 7 +++++++ config.php | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3786b0b..2d7ad19 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,13 @@ echo Config::get('DB_NAME'); echo Config::get('API_KEY'); // output: sdfds6549sdf7SDFD@#$55 +// With under any namespace +echo \Config::get('API_KEY'); + +// call other way from anywhere +getConfig('API_KEY'); +getConfig('API_KEY', 'DEFAULT VALUE IF NOT FOUND'); + echo Config::get('DUMMY', 'Default Value'); // output: Default Value // Get default value if key not found in config file diff --git a/config.php b/config.php index e8fb2fb..8fa4ca7 100644 --- a/config.php +++ b/config.php @@ -12,8 +12,8 @@ * */ -class Config -{ +class Config { + /** * store all list of configuration * @@ -122,4 +122,15 @@ private static function readFile($filePath) // return all lines return $lines; } +} + +/** + * add `getConfig()` function for configuration call without namespace call + * function can be call from anywhere global + * + */ +if(!function_exists("getConfig")) { + function getConfig($key, $defaultValue = null) { + return Config::get($key, $defaultValue); + } } \ No newline at end of file