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:
customExceptions.php
.myClassException.php
, in a directory called /exceptions
.Store all classes separately, one class - one file. Name of class should be name of containing file also (verbatim, case sensitive).
Use namespaces in format "vendor/application/..." - it's VERY handy thing, I promise.
More about it