3

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

gawpertron
  • 1,867
  • 3
  • 21
  • 37
  • IMO it would be an `InvalidArgumentException` because the argument passed to the autoloader is invalid (it specifies an invalid class name). Although I'm not certain of this, since docs seem to indicate this should be used for arguments of an invalid *type*. – DaveRandom Aug 10 '12 at 10:22
  • I agree; from an autoloader that would be an invalid argument. but what if you are throwing an exception after checking a class name using `class_exists()`? – gawpertron Aug 10 '12 at 10:30
  • @gawpertron again you will do `class_exists("MyClass")` where you pass as an argument if class doesn't exists then throw `InvalidArgument` else a `RuntimeException` i think is good to throw.. – Gntem Aug 10 '12 at 10:36
  • 2
    `RuntimeException` says `Exception thrown if an error which can only be found on runtime occurs.` - I disagree with this for a class-not-found error. If the class does not exist, this *could* be detected before runtime. But it depends on your definition of "detected" - the manual is a little unclear on this point. I personally feel it makes more sense to throw `InvalidArgumentException` because whether you are attempting to autoload or simply doing `class_exists()`, the string you passed represents an invalid class name and is therefore an invalid argument. – DaveRandom Aug 10 '12 at 10:50
  • 1
    check out this blog post: http://codeutopia.net/blog/2011/05/06/how-to-use-built-in-spl-exception-classes-for-better-error-handling/ It can give you some clues. – Xerkus Aug 11 '12 at 08:45
  • I came across that too. It certainly helps when to use types of exception. – gawpertron Aug 13 '12 at 21:44
  • @DaveRandom what's your definition of Runtime? do you have an example of checking whether a class exist before runtime? – gawpertron Aug 13 '12 at 21:47
  • 1
    Well I would interpret it fairly literally - the time at which (or *during* which) the program is running. In PHP this tends to be extremely short-lived because of the very nature of what it does. If your application is static and developed only by you, you (or your IDE) can detect whether the class does not exist by looking to see if it is there. The more I think about this, if you are bothering to check if a class exists you are presumably allowing much more dynamic behaviour and `RuntimeException` may in fact be more appropriate. But if you overthink it then *everything* is a runtime error. – DaveRandom Aug 13 '12 at 22:08

2 Answers2

2

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

gawpertron
  • 1,867
  • 3
  • 21
  • 37
Maks3w
  • 6,014
  • 6
  • 37
  • 42
-1

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.

Jerry Saravia
  • 3,737
  • 3
  • 24
  • 39
  • 1
    PSR-4 says the opposite of this answer. ["Autoloader implementations MUST NOT throw exceptions, MUST NOT raise errors of any level, and SHOULD NOT return a value."](http://www.php-fig.org/psr/psr-4/) This is not to say that the application can't throw an exception, but the application's autoloader never should. – Seth Battin Dec 11 '15 at 15:47
  • I'm a fan of the PSRs so what Seth said. – Jerry Saravia Dec 30 '15 at 15:11