What is the difference return bool return array and Exception?

0 like 0 dislike
20 views
Guys, that's what I wanted to discuss.

1. Earlier methods have returned true and false.
if($obj->method()){ echo 'All is well'; }else{ echo 'Error'; }


2. Next, replaced the bool on the Exception
try{ $obj->method(); echo 'All is well'; }catch(AppException $e){ echo $e->getMessage(); }


3. I have, once the method has returned (string) successful, or the error text
$result = $obj->method(); if($result == 'successful'){ echo 'All is well'; }else{ echo $result; }


And I honestly don't understand which of these methods is correct. If the first gave an error or not (without explanation), then the next two, like, doing 100%.
Can you explain how it should be ideally)
asked by | 20 views

4 Answers

0 like 0 dislike
You can return the error code and interpret. 0 - no errors 1 and next -error in accordance with the legend.
answered by
0 like 0 dislike
Here's another good article on the topic
answered by
0 like 0 dislike
All three cases are still in use. All depends on the situation.

Here is a good articledescribing the principle, when that return different error from exception.
answered by
0 like 0 dislike
Correct - try{}catch(){}. Accordingly, the class should throw Exception in any emergency, and code. who works with this class, all exceptions have processing.
answered by

Related questions

0 like 0 dislike
4 answers
0 like 0 dislike
4 answers
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,581 questions
257,150 answers
0 comments
476 users