Static Methods4:52 with Alena Holligan
Now that we have all the properties defined and methods for getting and setting those properties in a regulated manner, let’s complete the main action to display the recipe.
Great job getting and setting all those properties. 0:00 You don't have to set up everything you'll ever use right off the bat. 0:03 Most likely, ou'll be adding features as you enhance your program, 0:07 and that's great. 0:11 The next feature we should work on is a method for displaying the recipe. 0:12 We want a single method call that will pull all the information from the recipe 0:16 and give it to us in an easy to read format. 0:22 This was the first method we added to our class. 0:25 We called this method displayRecipe. 0:28 And now we're ready to finish this method. 0:30 This is a great opportunity to talk about another tenet of object-oriented 0:34 programming, single responsibility. 0:37 The single responsibility principle states that every class should have 0:40 responsibility over a single part of the functionality provided by the software. 0:44 And that responsibility should be entirely encapsulated or contained by the class. 0:50 A class should have only one reason to change. 0:57 Our Recipe class is in charge of describing that recipe itself. 1:00 Adding and retrieving data about a recipe are its main concerns. 1:06 Displaying a recipe is one way of retrieving data. 1:10 But that display could change based on where we're using that data, 1:14 not what data we'll be using. 1:18 This is a good place to break off into a new class. 1:21 Remember, a class is a way to organize our code and group the functionality and 1:24 data in a logical manner. 1:29 We'll put any methods that deal with the display or 1:32 rendering of data into a separate group. 1:35 Let's get coding. 1:38 Let's create a new class named Render. 1:39 To do that, we create a new file in our classes directory. 1:42 We'll name this render.php. 1:48 We add our opening php tags then create our new class, named render. 1:52 We can move our displayRecipe method over here. 2:03 We don't need to store any data in this class. 2:14 That's all handled by our recipe. 2:16 We just want to use this method on a particular recipe. 2:18 We can pass a recipe object into this method. 2:22 Let's add recipe. 2:25 Then we replace the keyword this with recipe. 2:29 Okay, now how do we use this method in our script? 2:36 Since we don't need to store any data or 2:40 keep the state of this class, we don't actually need to instantiate an object. 2:42 By declaring a method as static, we make it accessible 2:47 outside the class without needing to make an instance of the class. 2:51 To declare a method as static, we add the keyword static. 2:56 You can add this before or after the visibility, but 3:01 make sure you're consistent. 3:05 Back in our cookbook, we need to include our new class. 3:07 Let's clean up our output again. 3:18 Now we're ready to call our static method. 3:30 We've used static methods before with the PDO connection to a database. 3:33 To use a static method, we specify the class, Render, then double colons, 3:38 and the method, displayRecipe. 3:45 Then we'll pass in our recipe. 3:50 We want this to display to screen, so let's echo. 3:54 Now let's run our script. 3:59 Let's open this console a little further. 4:04 Now, let's run that again. 4:08 We see that we cannot access the private property 4:11 Recipe::$title in our render file. 4:14 That's right. 4:20 We made this private. 4:21 And because we're no longer calling it from the same class, 4:23 we'll need to specify our getter. 4:26 So getTitle and getSource. 4:29 These are methods, so we add our parentheses, and let's try that again. 4:36 Great, my first recipe by Alena Holligan. 4:43 Great job. 4:47 Now we're ready to expand our method to display the rest of the recipe details. 4:48
You need to sign up for Treehouse in order to download course files.Sign up