1 00:00:00,815 --> 00:00:05,112 [MUSIC] 2 00:00:05,112 --> 00:00:05,919 In many languages, 3 00:00:05,919 --> 00:00:09,160 an exception will be thrown whenever there is any sort of problem. 4 00:00:09,160 --> 00:00:12,200 These languages are what I would call exception-heavy. 5 00:00:12,200 --> 00:00:14,090 PHP is an exception-light language, 6 00:00:14,090 --> 00:00:15,960 which in most cases will output an arrow instead. 7 00:00:17,070 --> 00:00:20,760 Arrows can be output to the screen, local disk or both. 8 00:00:20,760 --> 00:00:24,240 PHP can also choose which arrow severities it will report and which it will ignore. 9 00:00:25,260 --> 00:00:30,290 The most common errors you run into are E_ERROR, E_NOTICE, and E_WARNING. 10 00:00:30,290 --> 00:00:34,200 Generally PHP will keep on executing despite throwing most types of errors. 11 00:00:34,200 --> 00:00:36,022 Though some severities like E_ERROR or 12 00:00:36,022 --> 00:00:39,910 E_CORE_ERROR will halt the program in an unrecoverable way. 13 00:00:39,910 --> 00:00:43,070 Some of this might feel like PHP telling you to eat your greens. 14 00:00:43,070 --> 00:00:44,680 Sure, it could let you get away with it but 15 00:00:44,680 --> 00:00:47,560 PHP knows that in the long run it's better to do things properly. 16 00:00:47,560 --> 00:00:50,490 For example, if a feature is marked as deprecated you probably want to 17 00:00:50,490 --> 00:00:52,150 know about it before it's removed. 18 00:00:52,150 --> 00:00:54,770 Otherwise an upgrade becomes much harder later on. 19 00:00:54,770 --> 00:00:58,860 So here is the index.php file that's completely full of broken stuff. 20 00:00:58,860 --> 00:01:02,579 On line four we can see the core error_reporting function being used. 21 00:01:03,750 --> 00:01:07,840 This function is how we control which severity or errors will be reported. 22 00:01:07,840 --> 00:01:11,814 And this code specifically requests that only E_ERROR, E_WARNING, and 23 00:01:11,814 --> 00:01:13,130 E_PATHS are reported. 24 00:01:14,180 --> 00:01:17,960 An error being reported means it will go into the log files and maybe be 25 00:01:17,960 --> 00:01:21,870 displayed on the screen, depending on the display errors option on line seven here. 26 00:01:23,460 --> 00:01:26,410 If we run this script in preview mode now we'll see two errors on 27 00:01:26,410 --> 00:01:28,540 the screen because we have display errors turned on. 28 00:01:28,540 --> 00:01:30,490 So, let's run that preview. 29 00:01:32,490 --> 00:01:34,140 So here we see int5. 30 00:01:34,140 --> 00:01:36,620 Some code seems to be working and we see some errors but 31 00:01:36,620 --> 00:01:38,710 they're all jammed up on the same line. 32 00:01:38,710 --> 00:01:42,410 This is because however this has been configured it's not 33 00:01:42,410 --> 00:01:43,700 outputting hours of HTML. 34 00:01:43,700 --> 00:01:45,380 It's outputting them with just text. 35 00:01:45,380 --> 00:01:47,810 So if we view the source we can see them more clearly. 36 00:01:47,810 --> 00:01:50,630 Now what's really strange is that I know from the code that I've written that 37 00:01:50,630 --> 00:01:53,655 there should be four errors because I intentionally broke a lot of stuff. 38 00:01:53,655 --> 00:01:55,760 Let's try taking a look at our code and see what could be wrong. 39 00:01:56,760 --> 00:02:00,590 So the first thing you might notice is that 5 is being added to 40 00:02:00,590 --> 00:02:04,510 a variable called nope and nowhere in this code is nope being defined. 41 00:02:04,510 --> 00:02:06,610 That should throw a notice, but we aren't seeing one. 42 00:02:06,610 --> 00:02:10,180 This is because we have not requested PHP to report notices and 43 00:02:10,180 --> 00:02:14,490 as such, an uninitialized variable will be treated as null without any complaints. 44 00:02:14,490 --> 00:02:17,340 5 plus null is 5, kinda. 45 00:02:17,340 --> 00:02:19,950 But it's not great to have that sort of code in our application. 46 00:02:19,950 --> 00:02:23,620 We don't want undefined variables roaming free without errors. 47 00:02:23,620 --> 00:02:24,570 Some developers pick and 48 00:02:24,570 --> 00:02:27,950 choose specific error levels like this, but it means that some might be forgotten. 49 00:02:27,950 --> 00:02:31,875 And it also means that newly added error severities like E_DEPRECATED and 50 00:02:31,875 --> 00:02:33,690 E_USER_DEPRECATED will be missed. 51 00:02:33,690 --> 00:02:37,360 Picking specific levels unless you really know what you're doing is dangerous. 52 00:02:37,360 --> 00:02:40,750 The general advice of many is to turn error_reporting up level up as high as 53 00:02:40,750 --> 00:02:42,510 you can using E_ALL. 54 00:02:42,510 --> 00:02:43,480 That's really easy to change. 55 00:02:43,480 --> 00:02:48,070 You just remove all of these specific levels and 56 00:02:48,070 --> 00:02:51,570 change it to E_ALL, and we can save that. 57 00:02:51,570 --> 00:02:57,140 If we go back to our view-source of the preview, we now see a lot more errors. 58 00:02:57,140 --> 00:02:58,370 These are the four I'm expecting. 59 00:02:58,370 --> 00:03:01,290 We can see here there's a notice of an undefined variable nope. 60 00:03:01,290 --> 00:03:04,598 It outputs 5 because it will still do the math, it will just warn you about it 61 00:03:04,598 --> 00:03:08,550 using a notice as a warning about creating a default object for an empty value. 62 00:03:08,550 --> 00:03:10,566 A strict standard error has happened, 63 00:03:10,566 --> 00:03:14,230 non-static method foo bar should not be called statically. 64 00:03:14,230 --> 00:03:16,030 And a fatal error, right at the end. 65 00:03:16,030 --> 00:03:18,880 Call to undefined method Foo_nope(). 66 00:03:18,880 --> 00:03:20,620 So let's see if we can fix this code really quickly. 67 00:03:23,010 --> 00:03:26,660 So, nope equals 0. 68 00:03:26,660 --> 00:03:29,380 That's one fixed. 69 00:03:31,350 --> 00:03:39,700 Now we can fix this warning by creating $wrestler equals new stdclass. 70 00:03:39,700 --> 00:03:41,490 That should fix that error. 71 00:03:43,080 --> 00:03:50,610 Public function bar, so this needs to be a static function to fix that error. 72 00:03:50,610 --> 00:03:55,390 And to get rid of the final error we need to make another function. 73 00:03:57,000 --> 00:03:59,020 Call it nope, see if this has worked. 74 00:04:02,520 --> 00:04:03,800 This should also be a static function. 75 00:04:06,790 --> 00:04:08,600 Perfect. The impossible has happened. 76 00:04:08,600 --> 00:04:12,560 We've got to the end, because we fixed all of our errors thanks to reporting. 77 00:04:13,870 --> 00:04:16,630 One thing to remember is that you never want your end users to see 78 00:04:16,630 --> 00:04:18,090 errors on the screen. 79 00:04:18,090 --> 00:04:21,140 In production, you want to have display errors turned off. 80 00:04:21,140 --> 00:04:24,440 If you have access to the servers, you can remove that any set line from your PHP, 81 00:04:24,440 --> 00:04:28,080 and rely on the server to decide if errors should be shown or not. 82 00:04:28,080 --> 00:04:29,770 Next, let's talk about exceptions.