php代碼這樣寫是不是多此一舉??

來源:互聯網
上載者:User
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());    }}}

這是摘出來的一段代碼。兩個問題
1:protected static $_useUploadTempDirectory = FALSE; 恒假,那麼if (self::$_useUploadTempDirectory)這個判斷就不成立,為什麼還要寫這個if語句呢?

2:if ( !function_exists('sys_get_temp_dir')) 這個if語句就在sys_get_temp_dir函數中,也就是說這個函數一直存在啊?? 那麼下面的語句也就不會執行了啊

不知道我理解的對不對?? 這個也算是一個比較成熟的cms裡的代碼了。。 為什麼會這麼寫呢??

回複內容:

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

這是摘出來的一段代碼。兩個問題
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?

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.