php configitem,CodeIgniter 源码里面关于config_item函数不理解的地方

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;

}

}

$_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

这里我针对函数引用返回,拓展个例子

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

传引用, 节省内存.

//它们都指向静态全局变量$config的zval

$config1 = &get_config();

$config2 = &get_config();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值