Was ich an der Funktion config_item im CodeIgniter-Quellcode nicht verstehe

WBOY
Freigeben: 2023-03-01 21:18:02
Original
1209 Leute haben es durchsucht

<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>
Nach dem Login kopieren
Nach dem Login kopieren
Ich verstehe die Funktion des

-Symbols in $_config[0] =& get_config();function &get_config(Array $replace = array()) und & nicht = = Gibt es hier eine besondere Funktion der Verwendung des &-Symbols = = Bitte antworten Sie ~

Antwortinhalt:

<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>
Nach dem Login kopieren
Nach dem Login kopieren
Ich verstehe die Funktion des

-Symbols in $_config[0] =& get_config();function &get_config(Array $replace = array()) und & nicht = = Gibt es hier eine besondere Funktion der Verwendung des &-Symbols = = Bitte antworten Sie ~

Referenzübergabe und Referenzrückgabe können bis zu einem gewissen Grad Speicherplatz sparen und auch indirekt den Zielwert ändern.
Offizielle Dokumentation zur Referenzübergabe: http://www.php.net/manual/zh/sprache.references.pass.php
Offizielle Dokumentation zur Referenzrückgabe: http://php.net/manual/zh /Sprache .references.return.php

Hier erweitere ich ein Beispiel für die Rückgabe von Funktionsreferenzen

<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>
Nach dem Login kopieren

Referenz übergeben, um Speicher zu sparen.

<code class="php">//它们都指向静态全局变量$config的zval
$config1 = &get_config();    
$config2 = &get_config();</code>
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!