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

[HELP] 在看CI源码的时候有这样一个问题,在URI类中

[复制链接]
发表于 2013-5-18 12:42:42 | 显示全部楼层 |阅读模式
这个是URI中 的一个方法
PHP复制代码
 
function _fetch_uri_string()
        {
                if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
                {
                        // Is the request coming from the command line?
                        if (php_sapi_name() == 'cli' or defined('STDIN'))
                        {
                                $this->_set_uri_string($this->_parse_cli_args());
                                return;
                        }
 
                        // Let's try the REQUEST_URI first, this will work in most situations
                        if ($uri = $this->_detect_uri())
                        {
                                $this->_set_uri_string($uri);
                                return;
                        }
 
                        // Is there a PATH_INFO variable?
                        // Note: some servers seem to have trouble with getenv() so we'll test it two ways
                        $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
                        if (trim($path, '/') != '' && $path != "/".SELF)
                        {
                                $this->_set_uri_string($path);
                                return;
                        }
 
                        // No PATH_INFO?... What about QUERY_STRING?
                        $path =  (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
                        if (trim($path, '/') != '')
                        {
                                $this->_set_uri_string($path);
                                return;
                        }
 
                        // As a last ditch effort lets try using the $_GET array
                        if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
                        {
                                $this->_set_uri_string(key($_GET));
                                return;
                        }
 
                        // We've exhausted all our options...
                        $this->uri_string = '';
                        return;
                }
 
                $uri = strtoupper($this->config->item('uri_protocol'));
 
                if ($uri == 'REQUEST_URI')
                {
                        $this->_set_uri_string($this->_detect_uri());
                        return;
                }
                elseif ($uri == 'CLI')
                {
                        $this->_set_uri_string($this->_parse_cli_args());
                        return;
                }
 
                $path = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
                $this->_set_uri_string($path);
        }
 
复制代码

 楼主| 发表于 2013-5-18 12:44:15 | 显示全部楼层
这是另一个方法
PHP复制代码
 
private function _detect_uri()
        {
                if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME']))
                {
                        return '';
                }
 
                $uri = $_SERVER['REQUEST_URI'];
                if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
                {
                        $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
                }
                elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
                {
                        $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
                }
 
                // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
                // URI is found, and also fixes the QUERY_STRING server var and $_GET array.
                if (strncmp($uri, '?/', 2) === 0)
                {
                        $uri = substr($uri, 2);
                }
                $parts = preg_split('#\?#i', $uri, 2);
                $uri = $parts[0];
                if (isset($parts[1]))
                {
                        $_SERVER['QUERY_STRING'] = $parts[1];
                        parse_str($_SERVER['QUERY_STRING'], $_GET);
                }
                else
                {
                        $_SERVER['QUERY_STRING'] = '';
                        $_GET = array();
                }
 
                if ($uri == '/' || empty($uri))
                {
                        return '/';
                }
 
                $uri = parse_url($uri, PHP_URL_PATH);
 
                // Do some final cleaning of the URI and return it
                return str_replace(array('//', '../'), '/', trim($uri, '/'));
        }
 
复制代码
 楼主| 发表于 2013-5-18 12:46:27 | 显示全部楼层
本帖最后由 优雅de凋零 于 2013-5-18 12:50 编辑

_fetch_uri_string() 的 13行有这样一个判断
PHP复制代码
 
if ($uri = $this->_detect_uri())
{
       $this->_set_uri_string($uri);
       return;
}
复制代码


这里调用了 第二个方法  _detect_uri()
在_detect_uri 方法中的return值是string
$uri = $this->_detect_uri () 这里一直为 true 则执行下面的语句
下面语句复制完后就 return了 下面的代码 一直不会执行 ...

我想代码是不会又有错的 经历了那么多的考验,最大的可能是我没有理解正确
但是小弟不才 ,是在是没有发现自己的错误,希望大家能帮我看下 ,是我哪里理解错了
多谢各位了!!!!!
发表于 2013-5-18 17:07:35 | 显示全部楼层
本帖最后由 dickfu 于 2013-5-18 17:08 编辑

空字符串在if里就是表现为false啊
 楼主| 发表于 2013-5-18 21:32:33 | 显示全部楼层
dickfu 发表于 2013-5-18 17:07
空字符串在if里就是表现为false啊

。。。
空字符串为 false???
我去试试....
发表于 2013-5-18 21:43:49 | 显示全部楼层
本帖最后由 浪迹天涯 于 2013-5-18 23:41 编辑

LZ说:
这里调用了 第二个方法  _detect_uri()
在_detect_uri 方法中的return值是string
$uri = $this->_detect_uri () 这里一直为 true 则执行下面的语句
下面语句复制完后就 return了 下面的代码 一直不会执行 ...


PHP复制代码
 
class test {
        public function test1()
        {
           if($uri = $this->get_val()){
               
           echo 'Call this function named `get_val`----'.$uri;
 
           }else{
               
           echo 'Empty';
 
           }
        }
    public function test2($uri = $this->get_val()  && 1){
             if($uri = $this->get_val()){              
                  echo 'Call this function named `get_val`----'.$uri;
 
           }else{
               
           echo 'Empty';
 
           }
    }
    private function get_val(){
 
        return 'test';
 
        // return '';
 
    }
}
 
复制代码

你试着运行test,test2这两个方法,然后在get_val()方法中改为return '',也就是注释掉//return 'test',看看分别运行的结果是什么
 楼主| 发表于 2013-5-18 22:03:28 | 显示全部楼层
浪迹天涯 发表于 2013-5-18 21:43
你试着运行test,test2这两个方法,然后在get_val()方法中改为return '',也就是注释掉//return 'test', ...

if('') if(0) if(NULL) if(false)
这四种都是被判断为 false
发表于 2013-5-18 22:11:19 | 显示全部楼层
优雅de凋零 发表于 2013-5-18 22:03
if('') if(0) if(NULL) if(false)
这四种都是被判断为 false

牛头不对马嘴
 楼主| 发表于 2013-5-18 23:29:20 | 显示全部楼层
看源码已经看晕了............
 楼主| 发表于 2013-5-18 23:29:49 | 显示全部楼层
觉得有必要把源码打印出来 看....

本版积分规则