PHP "Exception not found"

Go To StackoverFlow.com

12

I have a somehow funny issue. While trying to understand why a certain website returns http code 500 to browser, I found the message

PHP Fatal error:  Class 'MZ\\MailChimpBundle\\Services\\Exception' not found in /var/www/website/vendor/bundles/MZ/MailChimpBundle/Services/MailChimp.php on line 41

in apache log. Looking at the mentioned line:

throw new Exception('This bundle needs the cURL PHP extension.');

I now understand how to get the site working, but I still wonder why the code for throwing the exception (which would have resulted in a more helpful log message) failed. What could be the reason?

2012-04-03 20:05
by didi_X8
The class definition is missing, it's just class not found. Check if the sources contain the actual file and double-check if it's autoloader is configured correctly. As this is related to integration, it might be that this has not been fully tested by the vendor and \Exception was meant (PHP's native, global Exception class) instead - hakre 2012-04-03 20:08
but how can it fail if there's a global class with that name? Would a invokation of the global Exception class look different from this line? (I'm not a PHP pro, so maybe I lack basic knowledge here - didi_X8 2012-04-03 20:12
Yes, it would look like: throw new \Exception('This bundle ... as that line is namespaced. It's probably worth you check first if there is that exception class within the bundle code. If not, report a bug for that bundle - hakre 2012-04-03 20:13
so, the backslash forces usage of global namespace? If so, pls put this as answer, then I can vote it - didi_X8 2012-04-03 20:18


31

The MZMailChimpBundle does not contain a class named Exception within the MZ\MailChimpBundle\Services namespace.

Because of that simple fact and as the error message that the exception should signal is related to an integration problem (check for the curl library) I assume that this is a bug.

The original has meant \Exception and not Exception here. It's a somewhat common mistake that can happen with namespaces. To fix the file, either alias/import \Exception as Exception:

namespace MZ\MailChimpBundle\Services;
use Exception;

and/or change the new line in MZMailChimpBundle/Services/MailChimp.php:

throw new \Exception('This bundle needs the cURL PHP extension.');

See as well the related question: How to use “root” namespace of php? and the one with the same Class 'Namespace\Example' not found error message: Calling a static method from a class in another namespace in PHP.

2012-04-03 21:07
by hakre
great explanation, thanks - didi_X8 2012-04-04 16:51
+1 fixed my proble - Pastor Bones 2012-11-17 06:14
Has been fixed 2012-08-11 in 72297152 by miguel250hakre 2013-06-24 23:17


1

Looks to me that the line is trying to throw a user defined Exception in the current namespace, not the built-in Exception class of PHP itself

2012-04-03 20:18
by kDjakman
Ads