为什么 PHP 没有捕获“找不到类”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4421320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:01:51  来源:igfitidea点击:

Why doesn't PHP catch a "Class not found" error?

phptry-catch

提问by Edward Tanguay

In the following example, if the class does not exist, I want to catch the error and create a Nullclass instead.

在以下示例中,如果该类不存在,我想捕获错误并创建一个Null类。

But in spite of my try/catch statements, PHP simply tells me Class 'SmartFormasdfasdf' not found.

但是尽管我有 try/catch 语句,PHP 只是告诉我Class 'SmartFormasdfasdf' not found.

How can I get PHP to catch the 'class not found' error?

如何让 PHP 捕获“找不到类”错误?

<?php
class SmartFormLogin extends SmartForm {
    public function render() {
        echo '<p>this is the login form</p>';
    }
}

class SmartFormCodeWrapper extends SmartForm {
    public function render() {
        echo '<p>this is the code wrapper form</p>';
    }
}

class SmartFormNull extends SmartForm {
    public function render() {
        echo '<p>the form "' . htmlentities($this->idCode) . '" does not exist</p>';
    }
}

class SmartForm {

    protected $idCode;

    public function __construct($idCode) {
        $this->idCode = $idCode;
    }

    public static function create($smartFormIdCode) {
        $className = 'SmartForm' . $smartFormIdCode;
        try {
            return new $className($smartFormIdCode);
        } catch (Exception $ex) {
            return new SmartFormNull($smartformIdCode);
        }
    }
}

$formLogin = SmartForm::create('Login');
$formLogin->render();
$formLogin = SmartForm::create('CodeWrapper');
$formLogin->render();
$formLogin = SmartForm::create('asdfasdf');
$formLogin->render();
?>

Solution:

解决方案:

Thanks @Mchl, this is how I solved it then:

谢谢@Mchl,这就是我解决它的方法:

public static function create($smartFormIdCode) {
  $className = 'SmartForm' . $smartFormIdCode;
  if(class_exists($className)) {
    return new $className($smartFormIdCode);
  } else {
    return new SmartFormNull($smartFormIdCode);
  }
} 

回答by Mchl

Because it's a fatal error. Use class_exists() function to check if class exist.

因为这是一个致命的错误。使用 class_exists() 函数检查类是否存在。

Also: PHP is not Java - unless you redefined default error handler, it will raise errors and not throw exceptions.

另外:PHP 不是 Java - 除非您重新定义默认错误处理程序,否则它将引发错误而不是抛出异常。

回答by ChadSikorra

Old question, but in PHP7 this is a catchable exception. Though I still think the class_exists($class)is a more explicit way to do it. However, you could do a try/catch block using the new \Throwableexception type:

老问题,但在 PHP7 中,这是一个可捕获的异常。尽管我仍然认为这class_exists($class)是一种更明确的方法。但是,您可以使用新的\Throwable异常类型执行 try/catch 块:

$className = 'SmartForm' . $smartFormIdCode;
try {
    return new $className($smartFormIdCode);
} catch (\Throwable $ex) {
    return new SmartFormNull($smartformIdCode);
}

回答by Horace

php >= 7.0

php >= 7.0

php can catch 'class not found' as Throwable

php 可以将“找不到类”捕获为 Throwable

try {
        return new $className($smartFormIdCode);
} catch (\Throwable $ex) {
        return new SmartFormNull($smartformIdCode);
}

回答by John Parker

You need to use class_existsto see if the class exists before you try and instantiate it.

在尝试实例化它之前,您需要使用class_exists来查看该类是否存在。

Incidentally, if you're using a class autoloader, be sure to set the second arg to true.

顺便说一句,如果您使用的是类自动加载器,请务必将第二个参数设置为 true。

回答by dev-null-dweller

Because php emits fatal error when you ty to create new object of non existing class. To make it work you will need php >= 5.3 and autoloadfunction, where you should try to look for file with class definition or throw your custom exception.

因为当您创建非现有类的新对象时,php 会发出致命错误。要使其工作,您将需要 php >= 5.3 和自动加载功能,您应该尝试在其中查找具有类定义的文件或抛出您的自定义异常。