Home > Backend Development > PHP Tutorial > php代码这样写是不是多此一举??

php代码这样写是不是多此一举??

WBOY
Release: 2016-06-06 20:19:32
Original
1380 people have browsed it

<code>class PHPExcel_Shared_File
{
protected static $_useUploadTempDirectory    = FALSE;
public static function sys_get_temp_dir()
    {
        if (self::$_useUploadTempDirectory) {
            //  use upload-directory when defined to allow running on environments having very restricted
            //      open_basedir configs
            if (ini_get('upload_tmp_dir') !== FALSE) {
                if ($temp = ini_get('upload_tmp_dir')) {
                    if (file_exists($temp))
                        return realpath($temp);
                }
            }
        }

        // sys_get_temp_dir is only available since PHP 5.2.1
        // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
        if ( !function_exists('sys_get_temp_dir')) {
            if ($temp = getenv('TMP') ) {
                if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
            }
            if ($temp = getenv('TEMP') ) {
                if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
            }
            if ($temp = getenv('TMPDIR') ) {
                if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
            }

            // trick for creating a file in system's temporary dir
            // without knowing the path of the system's temporary dir
            $temp = tempnam(__FILE__, '');
            if (file_exists($temp)) {
                unlink($temp);
                return realpath(dirname($temp));
            }

            return null;
        }

        // use ordinary built-in PHP function
        //    There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
        //        be called if we're running 5.2.1 or earlier
        return realpath(sys_get_temp_dir());
    }

}
}</code>
Copy after login
Copy after login

这是摘出来的一段代码。两个问题
1:protected static $_useUploadTempDirectory = FALSE; 恒假,那么if (self::$_useUploadTempDirectory)这个判断就不成立,为什么还要写这个if语句呢?

2:if ( !function_exists('sys_get_temp_dir')) 这个if语句就在sys_get_temp_dir函数中,也就是说这个函数一直存在啊?? 那么下面的语句也就不会执行了啊

不知道我理解的对不对?? 这个也算是一个比较成熟的cms里的代码了。。 为什么会这么写呢??

回复内容:

<code>class PHPExcel_Shared_File
{
protected static $_useUploadTempDirectory    = FALSE;
public static function sys_get_temp_dir()
    {
        if (self::$_useUploadTempDirectory) {
            //  use upload-directory when defined to allow running on environments having very restricted
            //      open_basedir configs
            if (ini_get('upload_tmp_dir') !== FALSE) {
                if ($temp = ini_get('upload_tmp_dir')) {
                    if (file_exists($temp))
                        return realpath($temp);
                }
            }
        }

        // sys_get_temp_dir is only available since PHP 5.2.1
        // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
        if ( !function_exists('sys_get_temp_dir')) {
            if ($temp = getenv('TMP') ) {
                if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
            }
            if ($temp = getenv('TEMP') ) {
                if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
            }
            if ($temp = getenv('TMPDIR') ) {
                if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
            }

            // trick for creating a file in system's temporary dir
            // without knowing the path of the system's temporary dir
            $temp = tempnam(__FILE__, '');
            if (file_exists($temp)) {
                unlink($temp);
                return realpath(dirname($temp));
            }

            return null;
        }

        // use ordinary built-in PHP function
        //    There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
        //        be called if we're running 5.2.1 or earlier
        return realpath(sys_get_temp_dir());
    }

}
}</code>
Copy after login
Copy after login

这是摘出来的一段代码。两个问题
1:protected static $_useUploadTempDirectory = FALSE; 恒假,那么if (self::$_useUploadTempDirectory)这个判断就不成立,为什么还要写这个if语句呢?

2:if ( !function_exists('sys_get_temp_dir')) 这个if语句就在sys_get_temp_dir函数中,也就是说这个函数一直存在啊?? 那么下面的语句也就不会执行了啊

不知道我理解的对不对?? 这个也算是一个比较成熟的cms里的代码了。。 为什么会这么写呢??

1 static 只是静态 ,但是可以被修改的
2 sys_get_temp_dir 指的是php系统自带的函数,而不是类中的函数

有没有想过,它的继承类可能把$_useUploadTempDirectory设成true?

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template