Some of the earliest pitfalls beginning PHP developers have to overcome have to do with misunderstanding the differences between notices, warning, exceptions, and errors. They often seem like the same thing, but they're handling and resolution is drastically different. Even the best of us sometimes trip over error handling, but remembering and understanding the differences between these four features, and I would call PHP's error notifications features, can be more crucial to making good code than style or comments. Don't worry, it's easy.
A notice is probably the least serious issue that PHP can make you aware of. These are not necessarily errors, but they can easily cause errors or undesirable functionality. When you run a PHP script in the command line or by going to a website there are certain things that PHP will try to notify you about. Probably the most common notices are calling an element in an array that doesn't exist, or using a variable that has not been declared. Notices may not seem very serious, but it is incredibly bad form to leave any code in your scripts that you know is giving notices. In my experience, PHP has never been in the wrong by advising me to do something a different way by giving me a notice.
Warnings are errors that aren't very serious. PHP can let you know about these, but the script will continue to run. Things like calling methods without required arguments, trying to include files that aren't there, and uncaught exceptions (see below) will give you warnings. They're typically more serious than notices, but the script may continue to run just fine if they are not fixed. However, like notices, it is terribly bad form to leave any code that you are aware causes warning in your scripts, and they should be fixed with extreme priority.
Exceptions are the most commonly misunderstood feature in this article, and most of the pitfalls in error handling that learning developers come across stem from thinking of exceptions as like the other types of errors. Exceptions can be “handled,” and the script can continue to run as normal. This is one of those things that new developers tend to think is 'advanced' or complicated, but it's pretty simple. Here, I'll show you:
function classThatThrowsException($goodBadUgly) {
if($goodBadUgly == 'ugly') {
throw new Exception('Message for exception.');
}
}
try {
classThatThrowsException('ugly');
} catch(Exception $e) {
echo $e->getMessage();
// You can exit the script at this point, or just keep going and the rest of the script will run just fine.
// exit(1);
}
Those are the basics. Don't get me wrong, you can get really complicated with exceptions, but the gist of it is pretty straightforward. If the script gets to the code that “throws” the exception, the calling of that function “catches” it, and the class “Exception” has a function called “getMessage” that echos the string passed to the classes constructor in the “throw” statement. A lot of great functions in PHP, plugins, libraries, or snippets from other developers will throw exceptions, and if you ever receive a warning (see above, no recursion humor intended) telling you that you have an uncaught exception, you have to put that function call in a “try-catch” like in the snippet above. This feature in PHP allows for a great deal of the flexible and robust code reuse that makes repackageable solutions like Lexy Hosting stable and expandable. Also, if you have certain requirements for your functions, such as validating the arguments are in a certain format or something like that, it's a good idea to throw an exception when those requirements aren't met.
Warnings and exceptions will commonly be called errors, even notices, but in my experience calling something an error means an error that caused PHP to halt the execution of the script. I'm not one for arguing semantics, and you can call them whatever you want. There are many different types of errors, but as you're learning about them it's going to be much easier for you to simply consider the three categories above as something different than any error that halts the execution of your script. If you cause one of these to go off, your entire script fails and breaks. Handling these errors is less of a question of being a good developer as they are of developing anything at all.
Error handling can get more complicated as a site gets more complicated, but it is crucial that you fix all notices, warnings, and errors as well as catch and handle exceptions properly. As you're learning more and more about development, as even the most experienced of us constantly are, just remember to take things one step at a time. There's no need to get ahead of yourself, and, trust me, you'll regret it if you skim over something instead of learning it the right way from the start. Understanding these differences is crucial to basic development, as well as more advanced error handling that you may need to do in the future of a site.
VivaNet2.0 constructed website, myUSAi.org according to our exact technical specifications and with our required timeframe and budget. We liked VivaNet2.0...