简体   繁体   English

PHP致命错误:找不到类&#39;InvalidArgumentException&#39; <file> 在X线?

[英]PHP Fatal error: Class 'InvalidArgumentException' not found in <file> on line X?

I see this line in my php.log : 我在php.log看到这一行:

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

Line X contains this: 第X行包含:

throw new InvalidArgumentException("Error");

It happens on the page reload. 它发生在页面重新加载。 First, I put some values into a form, click submit and my page loads. 首先,我将一些值放入表单中,单击“提交”并加载我的页面。 I click Refresh Page (using my browser functions) to reload the page and this is where I get the error. 我单击刷新页面 (使用我的浏览器功能)重新加载页面,这是我收到错误的地方。

Maybe when I refresh the page, a different codepath is executed. 也许当我刷新页面时,会执行不同的代码路径。 But still I deem this to be a native PHP class that should always work, but in my case it throws the error. 但我仍然认为这是一个应该始终有效的本机PHP类,但在我的情况下,它会抛出错误。

I am using PHP, Apache2 on Windows, originally installed via Zend Studio IDE suite. 我在Windows上使用PHP,Apache2,最初是通过Zend Studio IDE套件安装的。

The class description itself does not offer me any hints. 类描述本身并没有给我任何提示。 Why does it throw the error and how do I fix it? 为什么会抛出错误,我该如何解决?

UPDATE * 更新 *

My code is: 我的代码是:

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

So it is whenever I hit this class I get this error. 所以无论什么时候我打这个课我都会遇到这个错误。 When I remove isset line and just throw the class directly, it always gives me the error. 当我删除isset行并直接抛出类时,它总是给我错误。

You are using a namespace for the class that the exception is being thrown from. 您正在为引发异常的类使用命名空间。 Because of this, PHP is looking for a class called "InvalidArgumentException" within that namespace. 正因为如此,PHP正在寻找一个名为“InvalidArgumentException”该命名空间的类。 However, the InvalidArgumentException exists in the Global Namespace. 但是,全局命名空间中存在InvalidArgumentException。 Therefore, in order to access it, simply prepend a backslash before the exception: 因此,为了访问它,只需在异常之前添加一个反斜杠:

throw new \InvalidArgumentException("Error");

尝试之前添加反斜杠。

throw new \InvalidArgumentException("Error");

尝试使用

use http\\Exception\\InvalidArgumentException;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM