Answer the question
In order to leave comments, you need to log in
Why, apart from the fact that “someone has already thought of it”, is it recommended to write a separate class for each action?
There is a complexity of the question, but, it was not easier to choose than simple or "stupid" in the list, sorry)
I still can't fully understand why the methods of the standard PHP exception, such, how can I say - inconvenient?
I have heard many opinions on this, but almost all of them are based on the fact that "people have already come up with, you just need to do as they have already come up with."
When I rely on this phrase, I remember thousands of libraries rewritten from scratch, because people washed down the lib, threw it into the network, and then - the community was fucked up there, here he just didn’t know and wanted to apply for a vacancy, here he didn’t consult with anyone, but here I just gave birth because I was tired.
standart:
throw new \HttpException500('Text'); // и array вообще нельзя
throw new \LogicException('Wtf? There is an ENGLISH string?'); // перевести? болт. array вообще нельзя
throw new \InvalidArgumentException('Wtf? There is an ENGLISH string?'); // перевести? болт. array вообще нельзя
throw new \MyLibrary\InvalidArgumentException('Wtf? There is an ENGLISH string?'); // перевести? болт. array вообще нельзя
throw (new \Ex(\MyLibrary\MyClass::ERR_NOT_FOUND))->data($array);
throw (new \Http(500))->data($array);
throw (new \Flash('validation.error', '@application.err.phrase', $placeholder1, $placeholder2))->data($array);
throw (new \Invalid('login', 'rule_login', $placeholder2))->data($array);
throw new \InvalidArgumentException(json_encode((array) $data));
foreach ($dto as $field => $value) {
$this->processString($value); // @throws
$this->processInt($value); // @throws
$this->processArray($value); // @throws
}
Answer the question
In order to leave comments, you need to log in
What Ivan Shumov wrote (points 1, 2) is correct. It is always easier to delimit exception classes in advance than to separate them into a separate class and refactor everywhere when the project logic requires an individual try-catch. Normal libs/packages/frameworks inherit their actions from the standard php ones, you can always catch the global \Exception or another parent if you don't need such detailed handling for each case.
Looks like I found some convenient way to use them. I'll post it here if anyone needs it.
Firstly, there is a standard there is some kind of PSR where there are 8 classes of "danger" of errors. But when I see a classification into 8 types, it looks like "we'll do it just in case" - a person is not able to divide something into 8 types, he keeps no more than 7 things in his head (and this is very pumped - 7 things). It is impossible to determine which of the 8 types of error is the one that now occurred to me. Maybe someone wrote in a book, but in life I think he does not do that.
If errors can be divided into 8 degrees, then the speech here is not in degrees, but in priority. That is, \SplPriorityQueue with a priority number, which, in which case, you can always change, and see the difference in statistics over a period of time. In this case, 8 types mean something like color markers in production - "open the red box, put things in the blue box", only without coloring, but in general 8 is too much for an unambiguous definition, and enough for statistics, although at the same time not.
We divide the emerging jambs into:
1. notification of the developer that the program is a curve and so it should not have been
2. notification / redirection of the user
3. notification of other modules in neighboring folders that something happened that they could solve, but in alone
The first group mainly includes standard exceptions. We write a function
function a() : int
{
return 'vasia';
}
function verifyEmail($email) : ? string
{
if (filter_var($email, FILTER_VALIDATE_EMAIL)) return null;
return __FUNCTION__ . ' fails: ' . $email;
}
// ... code
if ($err = verifyEmail(123)) throw new \UnexpectedValueException($err);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question