Magic Methods5:05 with Alena Holligan
Object Oriented PHP offers several "magic methods". The "magic" comes from the fact that they are triggered by an action instead of called directly. We’ll demonstrate the power of several of the magic methods you’ll encounter most often.
- Add more items to the __construct method.
- Add a different Magic Method to your class
Sir Arthur C Clark wrote, 0:00 any sufficiently advanced technology is indistinguishable from magic. 0:02 To a non programmer, programming is magic. 0:08 Magic can be defined as the power of influencing the course of events 0:11 by using mysterious forces. 0:16 So they're absolutely right. 0:19 Programming is magic. 0:20 Along with the magic that is programming itself, 0:22 PHP has something called magic methods. 0:25 You define magic methods in the same way you define other methods. 0:28 The magic behind these magic methods is that you never directly call them. 0:33 They're triggered by certain events in the program. 0:38 This allows us to do some pretty powerful things. 0:41 We'll take a look at some of the most common uses for 0:44 magic methods, and I'll add links to the teachers notes to learn more. 0:47 Let's start some magic. 0:52 Let's add a new magic method to our recipe class. 0:55 We add a magic method just like any other method. 0:58 But this time, we're going to use a specific name. 1:01 All magic methods start with the double underscore the first one we'll be 1:09 using is the __construct method. 1:14 This method is called at the time an object is constructed or created. 1:18 If we look at our cookbook, we see that we create a new object from our recipe class. 1:23 We then add the title to our recipe. 1:30 Our recipe isn't very useful without a title. 1:33 So we can set that title at the same time as we create an object. 1:35 Back in recipes, let's add a title parameter to our construct. 1:40 If we want someone to be able to create a recipe without adding a title right away, 1:47 we would need to set the default to null. 1:51 Then we call our set title method. 1:56 Our set title method will uppercase the words for anything, even null. 2:06 So our value won't actually be set to null. 2:11 We could perform the logic here and then construct. 2:14 But I want to do the same thing if we explicitly set the title. 2:17 So we can update the set title method and affect both of these methods. 2:21 We want to set the title value to null if a null value or an empty value is passed. 2:29 So if empty title. 2:34 Then we want to set this title = null. 2:41 Else set the title as we did before. 2:48 Back in our cookbook, we can now combine these two lines. 2:54 We passed the title directly to the call for new recipe. 3:11 Now let's run the script. 3:14 There we go. 3:28 Now we can see that nothing has changed. 3:29 And our recipe object still has the title. 3:31 We could pass as many arguments as we want to our construct. 3:34 And write any logic we want into the method, 3:38 just like we can in any other method. 3:40 But we'll leave it like this for now. 3:42 There's one more magic method I want to cover that you'll often see, 3:44 the Two String method. 3:48 Let's demonstrate how this will work. 3:50 At the end of the file, we call the display recipe. 3:53 But what would happen if we called recipe1 without a method? 3:57 Object of class recipe could not be converted to a string. 4:09 Essentially, what the two string method does is allow us to specify 4:14 how we want to convert this object to a string. 4:19 Let's go back and add that method. 4:22 Remember, magic methods start with a double underscore. 4:31 toString. 4:35 For the recipe, if we call the object directly, 4:41 we probably want to know what recipe this is. 4:44 So let's return. 4:47 this getTitle. 4:48 Now let's run our script again. 4:54 Now we see our recipe title displayed twice. 4:58 First for the two string, and then for the display recipe. 5:01
You need to sign up for Treehouse in order to download course files.Sign up