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

后端 未结 5 1072
不知归路
不知归路 2020-12-30 20:20

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

But in spite of my try/catch statement

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 20:50

    php >= 7.0

    php can catch 'class not found' as Throwable

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

提交回复
热议问题