用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2432|回复: 4
收起左侧

[已解决] 关于CodeIgniter的函数请教

[复制链接]
发表于 2012-2-15 17:52:39 | 显示全部楼层 |阅读模式
请问CodeIgniter中函数名前面都有这样的一个符号'&',我知道只是一个引用符,但是不知道在CodeIgniter里在定义函数时在函数前面加这个符号可以达到什么样的效果?



发表于 2012-2-15 18:39:26 | 显示全部楼层
定义函数的时候哪有用到这个符号呀?
把代码贴出来看看。
 楼主| 发表于 2012-2-15 18:40:54 | 显示全部楼层
if ( ! function_exists('get_config'))
{
        function &get_config($replace = array())
        {
                static $_config;

                if (isset($_config))
                {
                        return $_config[0];
                }

                // Is the config file in the environment folder?
                if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
                {
                        $file_path = APPPATH.'config/config.php';
                }

                // Fetch the config file
                if ( ! file_exists($file_path))
                {
                        exit('The configuration file does not exist.');
                }

                require($file_path);

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

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

                return $_config[0] =& $config;
        }
}
发表于 2012-2-15 18:44:11 | 显示全部楼层
这里 & 的意思是返回值是引用传递,也就是修改返回值会影响函数里面的 $_config。
 楼主| 发表于 2012-2-15 18:45:19 | 显示全部楼层
还有一个问题,这个$replace数组时怎么得到config.php文件里定义的数组内容的?

本版积分规则