Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:如何;空";班级成绩_Php_Function_Class - Fatal编程技术网

PHP:如何;空";班级成绩

PHP:如何;空";班级成绩,php,function,class,Php,Function,Class,我得到了一个如下的函数: class ProcessFile { function Function1() { ... } function Function2() { ... } ... } 我只想在文件扩展名不是“pdf”时使用该函数: if (substr(strrchr(strtolower($filename),'.'),1) != 'pdf') { $file = new ProcessFile($fil

我得到了一个如下的函数:

class ProcessFile {
    function Function1() {
        ...
    }
    function Function2() {
        ...
    }
    ...
}
我只想在文件扩展名不是“pdf”时使用该函数:

if (substr(strrchr(strtolower($filename),'.'),1) != 'pdf') {
   $file = new ProcessFile($filename);
} else {
   $file = null; // Just a test
}
我关心的是,在本页的后面,我在许多地方调用了类似的函数:

if (!empty($file->Function1())) {
   $var = $file ->Function1();
}
这会引发一个错误:

PHP致命错误:未捕获错误:调用成员函数 null上的函数1()

我希望避免在第一次“如果”测试中进行所有调用。在读取ProcessFille的结果或检查成员函数是否存在之前,如何将其设置为“null”?

在if语句中使用:

if (is_object($file)) {
   $var = $file ->Function1();
}
在if语句中使用:

if (is_object($file)) {
   $var = $file ->Function1();
}
您可以使用
isset($file)
检查
$file
是否为空:

if (isset($file)) {
   $var = $file ->Function1();
}
您可以使用
isset($file)
检查
$file
是否为空:

if (isset($file)) {
   $var = $file ->Function1();
}
如果($file!==null)
如果($file!==null)