优雅de凋零 发表于 2013-5-18 12:42:42

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

这个是URI中 的一个方法

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);
        }

优雅de凋零 发表于 2013-5-18 12:44:15

这是另一个方法

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;
                if (isset($parts))
                {
                        $_SERVER['QUERY_STRING'] = $parts;
                        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, '/'));
        }

优雅de凋零 发表于 2013-5-18 12:46:27

本帖最后由 优雅de凋零 于 2013-5-18 12:50 编辑

_fetch_uri_string() 的 13行有这样一个判断

if ($uri = $this->_detect_uri())
{
       $this->_set_uri_string($uri);
       return;
}

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

我想代码是不会又有错的 经历了那么多的考验,最大的可能是我没有理解正确
但是小弟不才 ,是在是没有发现自己的错误,希望大家能帮我看下 ,是我哪里理解错了
多谢各位了!!!!!

dickfu 发表于 2013-5-18 17:07:35

本帖最后由 dickfu 于 2013-5-18 17:08 编辑

空字符串在if里就是表现为false啊

优雅de凋零 发表于 2013-5-18 21:32:33

dickfu 发表于 2013-5-18 17:07 static/image/common/back.gif
空字符串在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了 下面的代码 一直不会执行 ...



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',看看分别运行的结果是什么

优雅de凋零 发表于 2013-5-18 22:03:28

浪迹天涯 发表于 2013-5-18 21:43 static/image/common/back.gif
你试着运行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 static/image/common/back.gif
if('') if(0) if(NULL) if(false)
这四种都是被判断为 false

牛头不对马嘴

优雅de凋零 发表于 2013-5-18 23:29:20

看源码已经看晕了............

优雅de凋零 发表于 2013-5-18 23:29:49

觉得有必要把源码打印出来 看....
页: [1]
查看完整版本: 在看CI源码的时候有这样一个问题,在URI类中