我在我的 php.log
中看到这一行:
PHP Fatal error: Class 'MyNamespace\InvalidArgumentException' not found in <file> on line X
X 行包含以下内容:
throw new InvalidArgumentException("Error");
它发生在页面重新加载时。首先,我将一些值放入表单中,单击提交并加载我的页面。我单击刷新页面(使用我的浏览器功能)重新加载页面,这就是我收到错误的地方。
也许当我刷新页面时,执行了不同的代码路径。但我仍然认为这是一个 native PHP 类,应该始终有效,但在我的情况下它会抛出错误。
我在 Windows 上使用 PHP、Apache2,最初是通过 Zend Studio IDE 套件安装的。
class description本身不给我任何提示。为什么会抛出错误,我该如何解决?
更新*
我的代码是:
if (!isset($modelNumber)) //$modelNumber is NULL when I refresh
throw new InvalidArgumentException("Error");
所以每当我上这个课时,我都会收到这个错误。当我删除 isset
行并直接抛出类时,它总是给我错误。
最佳答案
您正在为引发异常的类使用命名空间。因此,PHP 正在该命名空间 中寻找名为“InvalidArgumentException”的类。但是,InvalidArgumentException 存在于全局命名空间中。因此,为了访问它,只需在异常前添加一个反斜杠:
throw new \InvalidArgumentException("Error");
关于PHP fatal error : Class 'InvalidArgumentException' not found in <file> on line X?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22664695/