20 comments

  1. my simplified version of the class config:

    class config{
    public static function get($path,$value){
    $config = $GLOBALS['config'][$path][$value];
    return $config;
    }
    };

    if you want to get the 127.0.0.1 you do:
    config::get("mysql","host");

    and so on..

  2. if you want to check if the property you entered is exist or not you can just add else statement like this
    class config {
    public static function get($path = null) {
    if ($path) {
    $config = $GLOBALS['config'];
    $path = explode('/', $path);
    foreach ($path as $bit) {
    if (isset($config[$bit])) {
    $config = $config[$bit];
    } else {
    echo "the property $bit you're trying to access is not exist";
    }
    }
    return $config;
    }
    return false;
    }
    }

  3. Okay guys, to everyone who is getting the following error
    Notice: Array to string conversion in… AND EXPLANATION
    The problem is that your return $config; 99% is in wrong place. It must be after the foreach loop closing bracket. Like this:

    class Config
    {
    public static function get($path = null) {
    if($path) {
    $config = $GLOBALS['config'];
    $path = explode('/', $path);
    foreach($path as $bit) {
    if(isset($config[$bit])) {
    $config = $config[$bit];
    }
    }
    return $config;
    }
    return false;
    }
    }

    END
    Here is detailed commented version for those who didn't fully understand something πŸ™‚

    class Config
    {
    public static function get($path = null) { // 'Get' function with $path parameter
    if($path) { // Check if $path is not null
    $config = $GLOBALS['config']; // Create $config variable that stores the config array in init.php
    $path = explode('/', $path); // Take the passed $path parameter which is string separated by / and create

    an array from it

    foreach($path as $bit) { // Loop through the new $path array
    if(isset($config[$bit])) { // Check if the $config variable matches value from $path array

    $config = $config[$bit]; // Set the $config variable to the asked config array value, basically remove

    whole $config array and only leave the array part THAT matches the $path array value.

    }
    } // Check again if you passed more in $path and change $config accordingly
    return $config; // Return the newly set value of the $config from foreach loop
    }
    return false;
    }
    }

    Happy coding!

  4. Hi can anybody help using sublime text 3, my index.php file after echo config etc // 127.0.0.1 looks like a comment and also having problems and requested server not found on this server. any help would be greatly appreciated πŸ™‚

  5. I am trying this out for the first time and I got the following error: Undefined index: config in C:xampphtdocsprojectstestclassesConfig.php on line 7. I am really stuck. Any info would be appreciated.

  6. Parse error: syntax error, unexpected ''; ' (T_CONSTANT_ENCAPSED_STRING) in C:xampphtdocscoreinit.php on line 25 – i have went through the code 100 times cant see any errors with it can someone help πŸ™

  7. I've wasted a half hour trying to understand this function, Finally I got it
    It's soooo easy, I think I was drunk.
    Simply, The Conflic was the second loop
    To understand this print_r($config[$bit]); It will output the follwing array:
    Array
    (
    [host] => 127.0.0.1
    [username] => root
    [password] =>
    [db] => loginSystem
    )
    The second loop is returning host right ?
    So it will return 127.0.0.1 that's all.

  8. thankyou for tuitorial, can i get this code? am getting this error "This page isn’t working
    localhost is currently unable to handle this request.
    HTTP ERROR 500"

  9. You should follow a general course about PHP OOP before checking tutorial on specific feature such as login/register system, you will find it then very easy. Dont try to learn how to cook chocolate cake before learning how to breath.

Comments are closed.