CodeIgniter 原始碼裡面關於config_item函數不理解的地方

WBOY
發布: 2023-03-01 21:18:02
原創
1205 人瀏覽過

<code class="php">if ( ! function_exists('get_config'))
{
    /**
     * Loads the main config.php file
     *
     * This function lets us grab the config file even if the Config class
     * hasn't been instantiated yet
     *
     * @param    array
     * @return    array
     */
    function &get_config(Array $replace = array())
    {
        static $config;

        if (empty($config))
        {
            $file_path = APPPATH.'config/config.php';
            $found = FALSE;
            if (file_exists($file_path))
            {
                $found = TRUE;
                require($file_path);
            }

            // Is the config file in the environment folder?
            if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
            {
                require($file_path);
            }
            elseif ( ! $found)
            {
                set_status_header(503);
                echo 'The configuration file does not exist.';
                exit(3); // EXIT_CONFIG
            }

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

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

        return $config;
    }
}

// ------------------------------------------------------------------------

if ( ! function_exists('config_item'))
{
    /**
     * Returns the specified config item
     *
     * @param    string
     * @return    mixed
     */
    function config_item($item)
    {
        static $_config;

        if (empty($_config))
        {
            // references cannot be directly assigned to static variables, so we use an array
            $_config[0] =& get_config();
        }

        return isset($_config[0][$item]) ? $_config[0][$item] : NULL;
    }
}</code>
登入後複製
登入後複製

$_config[0] =& get_config();function &get_config(Array $replace = array())裡面&符號的作用不太理解= =這裡使用&裡面

&符號的作用不太理解= =這裡使用

&

符號有什麼特別的作用麼= =求解~

回覆內容:

<code class="php">if ( ! function_exists('get_config'))
{
    /**
     * Loads the main config.php file
     *
     * This function lets us grab the config file even if the Config class
     * hasn't been instantiated yet
     *
     * @param    array
     * @return    array
     */
    function &get_config(Array $replace = array())
    {
        static $config;

        if (empty($config))
        {
            $file_path = APPPATH.'config/config.php';
            $found = FALSE;
            if (file_exists($file_path))
            {
                $found = TRUE;
                require($file_path);
            }

            // Is the config file in the environment folder?
            if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
            {
                require($file_path);
            }
            elseif ( ! $found)
            {
                set_status_header(503);
                echo 'The configuration file does not exist.';
                exit(3); // EXIT_CONFIG
            }

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

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

        return $config;
    }
}

// ------------------------------------------------------------------------

if ( ! function_exists('config_item'))
{
    /**
     * Returns the specified config item
     *
     * @param    string
     * @return    mixed
     */
    function config_item($item)
    {
        static $_config;

        if (empty($_config))
        {
            // references cannot be directly assigned to static variables, so we use an array
            $_config[0] =& get_config();
        }

        return isset($_config[0][$item]) ? $_config[0][$item] : NULL;
    }
}</code>
登入後複製
登入後複製
$_config[0] =& get_config();function &get_config(Array $replace = array())裡面

&

符號的作用不太理解= =這裡使用

&
裡面
&

符號的作用不太理解= =這裡使用

&

裡面

&

符號的作用不太理解= =這裡使用

&🎜有什麼特別的作用麼= =求解~🎜 🎜 🎜引用傳遞和引用返回,一定程度上能節省記憶體空間,同時還可以達到間接修改目標值的情況。 🎜引用傳遞官方文件:http://www.php.net/manual/zh/language.references.pass.php🎜引用回傳官方文件:http://php.net/manual/zh/language.references.return .php🎜 🎜這裡我針對函數引用返回,拓展個例子🎜
<code>function &get_config()
{
    static $config = 0;
    $config += 1;
    echo sprintf("config=%d\n",$config);
    return $config;
}

$config_item = get_config();
$config_item = 100;
$config_item = get_config();

$config_item = &get_config(); // 注意这里的&
$config_item = 100;
$config_item = get_config();

//输出
config=1
config=2
config=3
config=101</code>
登入後複製
🎜 🎜傳引用, 節省記憶體.🎜
<code class="php">//它们都指向静态全局变量$config的zval
$config1 = &get_config();    
$config2 = &get_config();</code>
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!