2010grldr 发表于 2012-2-15 17:52:39

关于CodeIgniter的函数请教

请问CodeIgniter中函数名前面都有这样的一个符号'&',我知道只是一个引用符,但是不知道在CodeIgniter里在定义函数时在函数前面加这个符号可以达到什么样的效果?



Hex 发表于 2012-2-15 18:39:26

定义函数的时候哪有用到这个符号呀?
把代码贴出来看看。

2010grldr 发表于 2012-2-15 18:40:54

if ( ! function_exists('get_config'))
{
        function &get_config($replace = array())
        {
                static $_config;

                if (isset($_config))
                {
                        return $_config;
                }

                // 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 =& $config;
        }
}

Hex 发表于 2012-2-15 18:44:11

这里 & 的意思是返回值是引用传递,也就是修改返回值会影响函数里面的 $_config。

2010grldr 发表于 2012-2-15 18:45:19

还有一个问题,这个$replace数组时怎么得到config.php文件里定义的数组内容的?
页: [1]
查看完整版本: 关于CodeIgniter的函数请教