Which PHP SPL Exception should you throw if a class doesn't exists? within the context of ZF2 coding standards for exceptions.

In the standards it say exceptions should extend from one of PHP's SPL Exceptions. My first guess would be that it's some kind of RuntimeException.

PHP SPL Exceptions

有帮助吗?

解决方案

Depends on the context of your application.

If you are receiving the class name as a method argument and you try to load that class in the same method then the you should use an InvalidArgumentException exception.

If you are receiving the class name from a field value or from a method then you should use RuntimeException

其他提示

If autoload didn't exist PHP would spits out a RunTimeException right? So your loader should do the same if it doesn't find the class. ( That's IF you're writing your own autoloader ) Based on the ZF2 Coding Standard, it should throw a RunTimeException that is WITHIN the exception namespace OF the loader.

Thus if your loader is in My\Loader then your runtime exception would be in My\Loader\Exception\RunTimeException.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top