Welcome Guest, Not a member yet? Register   Sign In
simple code fix to Common CI get_config()
#1

[eluser]jgetner[/eluser]
this is what CI currently has but a loop at the end is rather slow and php has a better function is place that is ran by C instead of a php loop.
Code:
/**
* Loads the main config.php file
*
* This function lets us grab the config file even if the Config class
* hasn't been instantiated yet
*
* @access    private
* @return    array
*/
    function &get;_config($replace = array())
    {
        static $_config;

        if (isset($_config))
        {
            return $_config[0];
        }

        // Is the config file in the environment folder?
        if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config'.EXT))
        {
            $file_path = APPPATH.'config/config'.EXT;
        }

        // Fetch the config file
        if ( ! file_exists($file_path))
        {
            exit('The configuration file does not exist.');
        }

        require($file_path);

        // Does the $config array exist in the file?
        if ( ! isset($config) OR ! is_array($config))
        {
            exit('Your config file does not appear to be formatted correctly.');
        }

        // Are any values being dynamically replaced?
        if (count($replace) > 0)
        {
            foreach ($replace as $key => $val)
            {
                if (isset($config[$key]))
                {
                    $config[$key] = $val;
                }
            }
        }

        return $_config[0] =& $config;
    }

should more be something like this using array_merge() instead of a foreach loop.

Code:
/**
* Loads the main config.php file
*
* This function lets us grab the config file even if the Config class
* hasn't been instantiated yet
*
* @access    private
* @return    array
*/
    function &get;_config($replace = array())
    {
        static $_config;

        if (isset($_config))
        {
            return $_config[0];
        }

        // Is the config file in the environment folder?
        if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config'.EXT))
        {
            $file_path = APPPATH.'config/config'.EXT;
        }

        // Fetch the config file
        if ( ! file_exists($file_path))
        {
            exit('The configuration file does not exist.');
        }

        require($file_path);

        // Does the $config array exist in the file?
        if ( ! isset($config) OR ! is_array($config))
        {
            exit('Your config file does not appear to be formatted correctly.');
        }

        // Are any values being dynamically replaced?
        if (count($replace) > 0)
        {
            return $_config[0] = array_merge($replace , & $config);
        }

        else
        {
            return $_config[0] = & $config;
        }
        
        return(FALSE);
    }
#2

[eluser]danmontgomery[/eluser]
should be:

Code:
// Are any values being dynamically replaced?
if (count($replace) > 0)
{
    $config = array_merge($config, $replace);
}

return $_config[0] =& $config;




Theme © iAndrew 2016 - Forum software by © MyBB