【问题标题】:PHP Fatal error: Class 'InvalidArgumentException' not found in <file> on line X?PHP 致命错误:在第 X 行的 <file> 中找不到类“InvalidArgumentException”?
【发布时间】:2014-03-26 14:51:18
【问题描述】:

我在我的php.log 中看到了这一行:

PHP Fatal error:  Class 'MyNamespace\InvalidArgumentException' not found in <file> on line X

第 X 行包含以下内容:

throw new InvalidArgumentException("Error");

它发生在页面重新加载时。首先,我将一些值放入表单中,单击提交并加载我的页面。我单击刷新页面(使用我的浏览器功能)重新加载页面,这就是我得到错误的地方。

也许当我刷新页面时,会执行不同的代码路径。但我仍然认为这是一个应该始终工作的原生 PHP 类,但在我的情况下它会引发错误。

我在 Windows 上使用 PHP、Apache2,最初是通过 Zend Studio IDE 套件安装的。

class description 本身并没有给我任何提示。为什么会抛出错误,我该如何解决?

更新*

我的代码是:

if (!isset($modelNumber)) //$modelNumber is NULL when I refresh
        throw new InvalidArgumentException("Error");

所以每当我上这门课时,我都会收到这个错误。当我删除 isset 行并直接抛出类时,它总是给我错误。

【问题讨论】:

    标签: php logging apache2


    【解决方案1】:

    您正在为引发异常的类使用命名空间。因此,PHP 正在该命名空间 寻找一个名为“InvalidArgumentException”的类。但是,全局命名空间中存在 InvalidArgumentException。因此,为了访问它,只需在异常前添加一个反斜杠:

    throw new \InvalidArgumentException("Error");
    

    【讨论】:

      【解决方案2】:

      尝试在之前添加反斜杠。

      throw new \InvalidArgumentException("Error");
      

      【讨论】:

        【解决方案3】:

        尝试使用

        use http\Exception\InvalidArgumentException;

        【讨论】:

        • 他为什么要那样做?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-24
        • 2016-08-19
        • 1970-01-01
        • 2015-07-30
        • 2016-02-19
        • 2015-03-20
        • 1970-01-01
        相关资源
        最近更新 更多