>  기사  >  백엔드 개발  >  CodeIgniter 소스 코드의 config_item 함수에 대해 이해가 안되는 점

CodeIgniter 소스 코드의 config_item 함수에 대해 이해가 안되는 점

WBOY
WBOY원래의
2016-12-01 00:56:261209검색

<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/언어.references.pass.php
참조 반환 공식 문서: http://php.net/manual/zh /언어 .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>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.