Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I see this line in my php.log:

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

Line X contains this:

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.

I am using PHP, Apache2 on Windows, originally installed via Zend Studio IDE suite.

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.

like image 566
Dennis Avatar asked Mar 26 '14 14:03

Dennis


3 Answers

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. However, the InvalidArgumentException exists in the Global Namespace. Therefore, in order to access it, simply prepend a backslash before the exception:

throw new \InvalidArgumentException("Error");
like image 72
seeARMS Avatar answered Nov 12 '22 07:11

seeARMS


Try adding a backslash before.

throw new \InvalidArgumentException("Error");
like image 32
David Avatar answered Nov 12 '22 09:11

David


Try using

use http\Exception\InvalidArgumentException;

like image 1
carloscastillop Avatar answered Nov 12 '22 07:11

carloscastillop