What I don't understand about the config_item function in the CodeIgniter source code

WBOY
Release: 2023-03-01 21:18:02
Original
1171 people have browsed it

<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>
Copy after login
Copy after login

$_config[0] =& get_config();andfunction &get_config(Array $replace = array())I don’t quite understand the role of the&symbols == What’s special about using the&symbols here Does it work = =Please answer~

Reply content:

<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>
Copy after login
Copy after login

$_config[0] =& get_config();andfunction &get_config(Array $replace = array())I don’t quite understand the role of the&symbols == What’s special about using the&symbols here Does it work = =Please answer~

Reference passing and reference return can save memory space to a certain extent, and can also indirectly modify the target value.
Reference pass official document: http://www.php.net/manual/zh/language.references.pass.php
Reference return official document: http://php.net/manual/zh/language.references.return .php

Here I will expand an example on function reference return

<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>
Copy after login

Pass reference to save memory.

<code class="php">//它们都指向静态全局变量$config的zval $config1 = &get_config(); $config2 = &get_config();</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!