1 00:00:00,650 --> 00:00:02,920 Sir Arthur C Clark wrote, 2 00:00:02,920 --> 00:00:07,360 any sufficiently advanced technology is indistinguishable from magic. 3 00:00:08,360 --> 00:00:11,820 To a non programmer, programming is magic. 4 00:00:11,820 --> 00:00:16,680 Magic can be defined as the power of influencing the course of events 5 00:00:16,680 --> 00:00:19,050 by using mysterious forces. 6 00:00:19,050 --> 00:00:20,810 So they're absolutely right. 7 00:00:20,810 --> 00:00:22,710 Programming is magic. 8 00:00:22,710 --> 00:00:25,770 Along with the magic that is programming itself, 9 00:00:25,770 --> 00:00:28,010 PHP has something called magic methods. 10 00:00:28,010 --> 00:00:33,160 You define magic methods in the same way you define other methods. 11 00:00:33,160 --> 00:00:38,080 The magic behind these magic methods is that you never directly call them. 12 00:00:38,080 --> 00:00:41,290 They're triggered by certain events in the program. 13 00:00:41,290 --> 00:00:44,260 This allows us to do some pretty powerful things. 14 00:00:44,260 --> 00:00:47,050 We'll take a look at some of the most common uses for 15 00:00:47,050 --> 00:00:52,280 magic methods, and I'll add links to the teacher's notes to learn more. 16 00:00:52,280 --> 00:00:53,350 Let's start some magic. 17 00:00:55,290 --> 00:00:58,380 Let's add a new magic method to our recipe class. 18 00:00:58,380 --> 00:01:01,390 We add a magic method just like any other method. 19 00:01:01,390 --> 00:01:03,814 But this time, we're going to use a specific name. 20 00:01:09,417 --> 00:01:14,053 All magic methods start with the double underscore the first one we'll be 21 00:01:14,053 --> 00:01:16,380 using is the __construct method. 22 00:01:18,300 --> 00:01:22,410 This method is called at the time an object is constructed or created. 23 00:01:23,460 --> 00:01:30,160 If we look at our cookbook, we see that we create a new object from our recipe class. 24 00:01:30,160 --> 00:01:32,200 We then add the title to our recipe. 25 00:01:33,290 --> 00:01:35,880 Our recipe isn't very useful without a title. 26 00:01:35,880 --> 00:01:40,010 So we can set that title at the same time as we create an object. 27 00:01:40,010 --> 00:01:43,830 Back in recipes, let's add a title parameter to our construct. 28 00:01:47,111 --> 00:01:51,921 If we want someone to be able to create a recipe without adding a title right away, 29 00:01:51,921 --> 00:01:54,350 we would need to set the default to null. 30 00:01:56,800 --> 00:01:58,643 Then we call our setTitle method. 31 00:02:06,320 --> 00:02:11,320 Our setTitle method will uppercase the words for anything, even null. 32 00:02:11,320 --> 00:02:14,490 So our value won't actually be set to null. 33 00:02:14,490 --> 00:02:17,390 We could perform the logic here and then construct. 34 00:02:17,390 --> 00:02:21,420 But I want to do the same thing if we explicitly set the title. 35 00:02:21,420 --> 00:02:26,150 So we can update the setTitle method and affect both of these methods. 36 00:02:29,240 --> 00:02:34,636 We want to set the title value to null if a null value or an empty value is passed. 37 00:02:34,636 --> 00:02:41,440 So if empty title. 38 00:02:41,440 --> 00:02:48,136 Then we want to set this title = null. 39 00:02:48,136 --> 00:02:52,900 Else set the title as we did before. 40 00:02:54,390 --> 00:02:57,319 Back in our cookbook, we can now combine these two lines. 41 00:03:11,442 --> 00:03:14,730 We passed the title directly to the call for new recipe. 42 00:03:14,730 --> 00:03:16,624 Now let's run the script. 43 00:03:28,297 --> 00:03:29,400 There we go. 44 00:03:29,400 --> 00:03:31,340 Now we can see that nothing has changed. 45 00:03:31,340 --> 00:03:34,390 And our recipe object still has the title. 46 00:03:34,390 --> 00:03:38,060 We could pass as many arguments as we want to our construct. 47 00:03:38,060 --> 00:03:40,750 And write any logic we want into the method, 48 00:03:40,750 --> 00:03:42,840 just like we can in any other method. 49 00:03:42,840 --> 00:03:44,880 But we'll leave it like this for now. 50 00:03:44,880 --> 00:03:48,580 There's one more magic method I want to cover that you'll often see, 51 00:03:48,580 --> 00:03:49,590 the toString method. 52 00:03:50,800 --> 00:03:52,180 Let's demonstrate how this will work. 53 00:03:53,500 --> 00:03:57,600 At the end of the file, we call the displayRecipe. 54 00:03:57,600 --> 00:04:03,067 But what would happen if we called recipe1 without a method? 55 00:04:09,869 --> 00:04:14,410 Object of class Recipe could not be converted to a string. 56 00:04:14,410 --> 00:04:19,120 Essentially, what the toString method does is allow us to specify 57 00:04:19,120 --> 00:04:21,550 how we want to convert this object to a string. 58 00:04:22,730 --> 00:04:24,103 Let's go back and add that method. 59 00:04:31,901 --> 00:04:34,670 Remember, magic methods start with a double underscore. 60 00:04:35,970 --> 00:04:36,650 toString. 61 00:04:41,440 --> 00:04:44,480 For the recipe, if we call the object directly, 62 00:04:44,480 --> 00:04:47,440 we probably want to know what recipe this is. 63 00:04:47,440 --> 00:04:48,578 So let's return. 64 00:04:48,578 --> 00:04:54,590 this getTitle. 65 00:04:54,590 --> 00:04:55,730 Now let's run our script again. 66 00:04:58,060 --> 00:05:01,420 Now we see our recipe title displayed twice. 67 00:05:01,420 --> 00:05:05,230 First for the toString, and then for the displayRecipe.