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

php中文网
发布: 2016-06-06 20:19:32
原创
1523人浏览过

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函数中,也就是说这个函数一直存在啊?? 那么下面的语句也就不会执行了啊

魔法映像企业网站管理系统
魔法映像企业网站管理系统

技术上面应用了三层结构,AJAX框架,URL重写等基础的开发。并用了动软的代码生成器及数据访问类,加进了一些自己用到的小功能,算是整理了一些自己的操作类。系统设计上面说不出用什么模式,大体设计是后台分两级分类,设置好一级之后,再设置二级并选择栏目类型,如内容,列表,上传文件,新窗口等。这样就可以生成无限多个二级分类,也就是网站栏目。对于扩展性来说,如果有新的需求可以直接加一个栏目类型并新加功能操作

魔法映像企业网站管理系统 0
查看详情 魔法映像企业网站管理系统

立即学习PHP免费学习笔记(深入)”;

不知道我理解的对不对?? 这个也算是一个比较成熟的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函数中,也就是说这个函数一直存在啊?? 那么下面的语句也就不会执行了啊

立即学习PHP免费学习笔记(深入)”;

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

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

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

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号