Until recently, I've done as shown below to create and throw custom exceptions:
File: Myclass.php:
namespace myapp\libraries;
class myclass {
setEmail($email) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new myClassException("Email not valid");
}
}
}
class myClassException extends \Exception {}
Ok, so this works well and are easy to maintain and build upon, provided that custom exceptions always exists in the same file and are defined after the class which throws the Exceptions.
Question: What does current PHP conventions and common sense say?
Here's the solutions I can think about:
- Keep declaring custom Exceptions immediately after the class that will invoke/throw it.
- Store all custom Exceptions in a mutually shared file, called
customExceptions.php
. - Store all custom Exceptions in a separate file named after its exception,
myClassException.php
, in a directory called/exceptions
.